From bd60c52c3c17b84f97f33db87caf8f49d715040b Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Tue, 21 Oct 2008 14:07:36 +0000 Subject: [1.0.X] Fixed #9039 -- Don't perform unique checks on NULL values, since NULL != NULL in SQL. Backport of [9239] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9240 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/model_forms/models.py | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 0b99a84cda..96b92a3233 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -145,7 +145,15 @@ class Inventory(models.Model): def __unicode__(self): return self.name - + +class Book(models.Model): + title = models.CharField(max_length=40) + author = models.ForeignKey(Writer, blank=True, null=True) + special_id = models.IntegerField(blank=True, null=True, unique=True) + + class Meta: + unique_together = ('title', 'author') + __test__ = {'API_TESTS': """ >>> from django import forms >>> from django.forms.models import ModelForm, model_to_dict @@ -1201,6 +1209,32 @@ False >>> form.is_valid() True +# Unique & unique together with null values +>>> class BookForm(ModelForm): +... class Meta: +... model = Book +>>> w = Writer.objects.get(name='Mike Royko') +>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk}) +>>> form.is_valid() +True +>>> form.save() + +>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk}) +>>> form.is_valid() +False +>>> form._errors +{'__all__': [u'Book with this Title and Author already exists.']} +>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'}) +>>> form.is_valid() +True +>>> form.save() + +>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'}) +>>> form.is_valid() +True +>>> form.save() + + # Choices on CharField and IntegerField >>> class ArticleForm(ModelForm): ... class Meta: -- cgit v1.3