Opened 11 years ago

Closed 11 years ago

#20742 closed Bug (needsinfo)

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

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 (last modified by Karen Tracey)

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@example.com')

form = TestForm(email='new@example.com', instance=x)

# here instance is updated with new@example.com 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 (2)

comment:1 by amin@…, 11 years ago

Summary: ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() chaning the instanceModelForm primary_key update with instance set is executed as INSERT caused by is_valid() changing the instance

comment:2 by Karen Tracey, 11 years ago

Description: modified (diff)
Resolution: needsinfo
Status: newclosed

A concrete suggestion for what's wanted here would help, as it is I'm not sure what's being asked for.

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