﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13079	unique_together constraint is ignored in form generated from model, when inheritance comes into play	Piotr Czachur	nobody	"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)
}}}

"		closed	Forms	1.2-beta		duplicate			Unreviewed	0	0	0	0	0	0
