Opened 15 years ago
Closed 15 years ago
#13079 closed (duplicate)
unique_together constraint is ignored in form generated from model, when inheritance comes into play
Reported by: | Piotr Czachur | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.2-beta |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Here you got test case that should pass, but it doesn't because unique_together constraint is ignored.
This happens when constraint is defined in parent class model, and there is child model extending that parent, and form is generated from child model.
Code is simple, just take a look if my explanation is messy.
from django.test import TestCase from django.db import models from django import forms import unittest class Person(models.Model): first_name = models.CharField(max_length=80) surname = models.CharField(max_length=80) class Meta: unique_together = (('first_name', 'surname'),) class ExtPerson(Person): phone = models.CharField(max_length=80) class PersonForm(forms.ModelForm): class Meta: model = Person class ExtPersonForm(forms.ModelForm): class Meta: model = ExtPerson class ExtPersonForm1(PersonForm): class Meta: model = ExtPerson class TestUniqueTogetherValidation(TestCase): def setUp(self): Person(first_name='Chuck', surname='Norris').save() def tearDown(self): Person.objects.all().delete() # works as expected def test_unique_together_validation_should_work_for_person(self): data = {'first_name':'Chuck', 'surname':'Norris'} form = PersonForm(data) self.assertFalse(form.is_valid()) # test will prove missing check for unique_together constrains from Person class def test_unique_together_validation_should_work_for_extperson(self): data = {'first_name':'Chuck', 'surname':'Norris', 'phone':'1234'} form = ExtPersonForm(data) self.assertFalse(form.is_valid()) # test will prove missing check for unique_together constrains from Person class def test_unique_together_validation_should_work_for_extperson_when_model_form_inherits_from_PersonForm(self): data = {'first_name':'Chuck', 'surname':'Norris', 'phone':'1234'} form = ExtPersonForm1(data) self.assertFalse(form.is_valid()) # test will prove missing check for unique_together constrains from Person class def test_unique_together_validation_return_true_when_data_is_valid_so_writing_to_database_should_work_too(self): data = {'first_name':'Chuck', 'surname':'Norris', 'phone':'1234'} form = ExtPersonForm(data) self.assertTrue(form.is_valid()) form.save()
Ran 4 tests in 0.219s FAILED (failures=2, errors=1)
Change History (3)
comment:1 by , 15 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 by , 15 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
comment:3 by , 15 years ago
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
Note:
See TracTickets
for help on using tickets.
I believe this is #12881.