Opened 11 years ago

Last modified 11 years ago

#20742 closed Bug

ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() chaning the instance — at Initial Version

Reported by: amin@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: modelform instance primary_key update
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I do agree this is a bad database design but it's executed without any warning or anything in the docs so i want to open this ticket to allow the discussion of raising an Error or anything (e.g. an info in the docs @primary_key or modelform) not let anyone run into this mistake.

class Test(models.Model):

email = models.CharField(max_length=100, primary_key=True)

class TestForm(forms.ModelForm):

class Meta:

model = Test

# assuming this object exists
x = Test.objects.get(email='old@…')

form = TestForm(email='new@…', instance=x)

# here instance is updated with new@… as pkey and django thinks it has to create a new model instance not updating the old instance
if form.is_valid():

new_x = form.save()
new_x.save()

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top