#20742 closed Bug (needsinfo)

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

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 kmtracey)

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 Changed 23 months ago by amin@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() chaning the instance to ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() changing the instance

comment:2 Changed 22 months ago by kmtracey

  • Description modified (diff)
  • Resolution set to needsinfo
  • Status changed from new to closed

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