Just to mention, English is not my native language.
So my question is, I have a form in Python/Django, that adds a simple object with name and function, example:
Function: programmer
Name: Carlos
What I want to do is to make things simple for the user when he adds several users with the same function, so I thought, when the user do something like this...
Function: programmer
Name: Dean,Seth,Roman,Ross,Joey,Chandler
...my form would add 6 objects, but the problem is that when I do this, python/django always tries to use the same ID/PK(example: tries to add all 6 members with id/pk=1).
I've done a little manuever(in Brasil we call gambiarra), so my code does this(after splitting objects):
form2.name = nombre
form2.pk = primary+1
form2.save()
primary=form2.pk
This basically consists in using this var called primary to change the value of PK and saving to Postgres. It worked, now Dean is pk/id 1....Chandler is pk/id 6, and all in one form.
But, I'm not that lucky, so when I go to add another name, like George, the system tries to use pk=2.
Does anyone knows to resolve this thing without this manuever or how to improve this manuever to solve completely?
My code (just to remind, I'm a python beginner):
objnet = form.save(commit=False)
if ',' in objnet.netaddr:
listaips = objnet.netaddr.split(',')
codeon = 1
nomecomum=objnet.nome
for ip in listaips:
objnet.netaddr = ip
objnet.nome = nomecomum + "_" + str(codeon)
objnet.save()
codeon+=1
else:
objnet.save()
Explanation:
I have a form with an IP field(netaddr) and a char field(nome)....if contains a ',' in IP field, it will add more than one object, example:
nome = carlos
netaddr = 2.2.2.2,3.3.3.3
it should add carlos_1 in my DB with IP 2.2.2.2, and carlos_2 with IP 3.3.3.3. And the code really does that, but, he adds carlos_1 and then, he tries to add carlos_2, he does with the same PK, so instead of adding another element, he overwrites carlos_1, so in my DB now there's only carlos_2.
Thank you
You have such problem because
objnetalways link to one object (sinceform.save()returns object)Quick fix: