Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14234 closed (fixed)

Validation bug when using ModelForms

Reported by: DavidReynolds Owned by: jdunck
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 jdunck 4 years ago.
dj14234-save-base.diff (523 bytes) - added by jdunck 4 years ago.
fast/hacky
dj14234-save-base_tests.diff (2.0 KB) - added by DavidReynolds 4 years ago.
Patch with tests included

Download all attachments as: .zip

Change History (10)

comment:1 Changed 4 years ago by jdunck

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to jdunck
  • Patch needs improvement unset
  • Status changed from new to assigned

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 4 years ago by jdunck

Changed 4 years ago by jdunck

fast/hacky

comment:2 Changed 4 years ago by jdunck

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

Set flags I missed.

Changed 4 years ago by DavidReynolds

Patch with tests included

comment:3 Changed 4 years ago by DavidReynolds

  • 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 4 years ago by dmoisset

  • Triage Stage changed from Unreviewed to Accepted

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

comment:5 Changed 4 years ago by jdunck

  • 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 4 years ago by carljm

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 4 years ago by carljm

(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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.