any ideas why this is happening?
(Pdb) import copy
(Pdb) tmpForm=copy.copy(form1)
(Pdb) form1
<cms.forms.MainFeaturedForm object at 0x7f05a0493350>
(Pdb) tmpForm
<cms.forms.MainFeaturedForm object at 0x7f05a054e950>
copying form1 to tmpForm (and making sure they’re on different memory addresses) to make sure this behaviour is not happening because form1 is being changed
(Pdb) v1=form1.save(commit=False)
(Pdb) v1.position
as can be seen above, v1.position == None right after a form1.save(commit=False)
(Pdb) v1.image_type=2
(Pdb) v1.Article=article
(Pdb) v1.section=33
(Pdb) v1.save()
(Pdb) v1.position
55L
here I set some values and saved it
the save() function changes v1 position to 55 (that’s expected)
(Pdb) v2=tmpForm.save(commit=False)
(Pdb) v2.position
55L
but now, after I saved v1, v2 (a completly new instance) has a position setted to the same one that was set on v1 (not expected)
in case it helps, here’s the save() function of this object (class Featured):
def save(self):
if self.Article:
try:
featured = Featured.objects.get(Article=self.Article, section=self.section)
self.hiddenID = featured.hiddenID
if self.position == None:
if featured.position == None:
self.position = 55
else:
self.position = featured.position
super(Featured, self).save(force_insert=False, force_update=True)
except Featured.DoesNotExist:
self.hiddenID = None
super(Featured, self).save(force_insert=True, force_update=False)
else:
self.hiddenID = None
super(Featured, self).save(force_insert=True, force_update=False)
Use copy.deepcopy() rather than copy.copy (wild guess: probably it’s sharing the data dict instance …).
That is incorrect. A modelform instance sets self.instance in init and then only works on that. If you copy the reference to form.instance and then modify it outside the form, it will of course be reflected on form.instance.
deepcopy vs. copy:
The thing is that copy will just copy a value, and the references it contains are preserved. deepcopy() copies a value and all the values it contains.