Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#14234 closed (fixed)

Validation bug when using ModelForms

Reported by: David Reynolds Owned by: Jeremy Dunck
Component: Forms Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I am running into a validation problem with ModelForms - here is a quick summary of what is happening

Take a simple model - here we call Model.full_clean as part of the custom save method to enforce the validation:

from django.db import models

class Stuff(models.Model):
   """(Stuff description)"""
   name = models.CharField(max_length=100)
   age = models.IntegerField()
   description = models.TextField(blank=True)

   def __unicode__(self):
       return self.name

   def save(self, *args, **kwargs):
       self.full_clean()
       return super(Stuff, self).save(*args, **kwargs)

Simple ModelForm for the above - nothing unusual here:

from django import forms


class StuffModelForm(forms.ModelForm):

   class Meta:
       model = Stuff

# Instantiate the form with some data
form = StuffModelForm({
   'name': 'Fred',
   'age': '56',
   'description': 'Old Fred is 57',
})

# Check the form is valid
if form.is_valid():
   obj = form.save(commit=False) # save but don't commit
   obj.age = 27 # Change the age
   obj.save() # Do a full save of the object

   obj.name = 'Ted' # Change the name
   obj.save() # Attempt to update the db record

# you get this error:
ValidationError: {'id': [u'Stuff with this ID already exists.']}

This happens because the ModelForm sets ModelForm.instance._adding to True and never cleans it up. This is then tested for in Model._perform_unique_checks and the uniqueness checks are executed if it is set to True which it always is after calling ModelForm.save()

Attachments (3)

dj14234-signal.diff (1.1 KB) - added by Jeremy Dunck 6 years ago.
dj14234-save-base.diff (523 bytes) - added by Jeremy Dunck 6 years ago.
fast/hacky
dj14234-save-base_tests.diff (2.0 KB) - added by David Reynolds 6 years ago.
Patch with tests included

Download all attachments as: .zip

Change History (10)

comment:1 Changed 6 years ago by Jeremy Dunck

Owner: changed from nobody to Jeremy Dunck
Status: newassigned

Though I am not sure putting full_clean in a model's .save is a good approach, I do agree with your assessment of the bug - that _adding should be reset.

Given that _adding is only set by BaseModelForm, I think it should be responsible for cleaning it up. That would suggest using a signal on post_save from BaseModelForm. Unfortunately, that's bad for performance since it means a lot of housekeeping, and maybe some apps have many active forms.

A slightly hackier way, but less code and definitely faster, would be just having Model.save_base reset ._adding.

I've attached (untested) patches for both. I'd lean toward the fast/hacky way, but if it works for you, please add tests so it can be committed.

Changed 6 years ago by Jeremy Dunck

Attachment: dj14234-signal.diff added

Changed 6 years ago by Jeremy Dunck

Attachment: dj14234-save-base.diff added

fast/hacky

comment:2 Changed 6 years ago by Jeremy Dunck

Has patch: set
Needs tests: set
Patch needs improvement: set

Set flags I missed.

Changed 6 years ago by David Reynolds

Patch with tests included

comment:3 Changed 6 years ago by David Reynolds

Has patch: unset
Needs tests: unset
Patch needs improvement: unset

Jeremy, it seemed massively overkill to use a signal for this, so I went with the simpler version and added a test for it.

comment:4 Changed 6 years ago by dmoisset

Triage Stage: UnreviewedAccepted

Given that a core committer proposed a patch, I'm assuming this should be flagged as "Accepted"

comment:5 Changed 6 years ago by Jeremy Dunck

Has patch: set

I'm not a core committer - just a long-time contributor. Even so, yes, I think this is ready for review.

comment:6 Changed 6 years ago by Carl Meyer

Resolution: fixed
Status: assignedclosed

(In [14612]) Fixed #14234 -- Re-validating a model instance added via ModelForm no longer throws spurious PK uniqueness errors. Thanks to David Reynolds and Jeremy Dunck.

Also moved Model._adding to Model._state.adding to reduce instance namespace footprint.

comment:7 Changed 6 years ago by Carl Meyer

(In [14615]) [1.2.X] Fixed #14234 -- Re-validating a model instance added via ModelForm no longer throws spurious PK uniqueness errors. Thanks to David Reynolds and Jeremy Dunck.

Also moved Model._adding to Model._state.adding to reduce instance namespace footprint.

Backport of r14612.

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