#18912: ModelForm doesn't handle unique checks for parent models.
---------------------------------+--------------------
     Reporter:  sebastian_noack  |      Owner:  nobody
         Type:  Bug              |     Status:  new
    Component:  Forms            |    Version:
     Severity:  Normal           |   Keywords:
 Triage Stage:  Unreviewed       |  Has patch:  1
Easy pickings:  0                |      UI/UX:  0
---------------------------------+--------------------
 The unique checks performed by the !ModelForm class, do not handle parent
 models. Unique fields inherited from a parent model are checked the same
 way as direct unique fields and therefore erroneously validates if there
 is a conflict in the parent model without a related object of the derived
 model.

 {{{#!python
 class Animal(models.Model):
     name = models.CharField(max_length=100, unique=True)

 class Mammal(Animal):
     pass

 class Bird(Animal):
     pass

 Bird.objects.create(name='Kiwi')

 class MammalForm(ModelForm):
     class Meta:
         model = Mammal

 form = MammalForm({'name': 'Kiwi'})
 if form.is_valid(): # Returns true, as there is no Mammal with the name
 'Kiwi'.
     form.save()     # Raises IntegrityError, because of there is already
 an Animal with the name 'Kiwi'.
 }}}

 Also unique_together definitions of parent models are completely ignored.

 {{{#!python
 class Animal(models.Model):
     type = models.IntegerField(choices=((1, 'Mammal'), (2, 'Bird')))
     name = models.CharField(max_length=100)

     class Meta:
         unique_together = (('type', 'name'),)

 class Mammal(Animal):
     pass

 class Bird(Animal):
     pass

 Mammal.objects.create(type=1, name='Cat')

 class MammalForm(ModelForm):
     class Meta:
         model = Mammal

 form = MammalForm({'type': 1, 'name': 'Cat'})
 if form.is_valid(): # Returns true, because of unique_together definitions
 from parent models are ignored.
     form.save()     # Raises IntegrityError, because of there is already
 an Animal of type 1 (Mammal) and the name 'Cat'.
 }}}

 My patch will fix that behavior, making validation fail in the cases
 above.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18912>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to