Fixed #9218 -- Simplified the fix from #9039 and added tests to ensure this case doesn't break again (and that the simplification didn't break anything).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9341 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e600364f7d
commit
21fa517248
|
@ -234,7 +234,7 @@ class BaseModelForm(BaseForm):
|
||||||
# equal NULL in SQL we should not do any unique checking for NULL values.
|
# equal NULL in SQL we should not do any unique checking for NULL values.
|
||||||
unique_checks = []
|
unique_checks = []
|
||||||
for check in self.instance._meta.unique_together[:]:
|
for check in self.instance._meta.unique_together[:]:
|
||||||
fields_on_form = [field for field in check if field in self.cleaned_data and not self.cleaned_data[field] is None]
|
fields_on_form = [field for field in check if self.cleaned_data.get(field) is not None]
|
||||||
if len(fields_on_form) == len(check):
|
if len(fields_on_form) == len(check):
|
||||||
unique_checks.append(check)
|
unique_checks.append(check)
|
||||||
|
|
||||||
|
@ -248,7 +248,7 @@ class BaseModelForm(BaseForm):
|
||||||
except FieldDoesNotExist:
|
except FieldDoesNotExist:
|
||||||
# This is an extra field that's not on the ModelForm, ignore it
|
# This is an extra field that's not on the ModelForm, ignore it
|
||||||
continue
|
continue
|
||||||
if f.unique and name in self.cleaned_data and not self.cleaned_data[name] is None:
|
if f.unique and self.cleaned_data.get(name) is not None:
|
||||||
unique_checks.append((name,))
|
unique_checks.append((name,))
|
||||||
|
|
||||||
bad_fields = set()
|
bad_fields = set()
|
||||||
|
|
|
@ -159,6 +159,15 @@ class Book(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('title', 'author')
|
unique_together = ('title', 'author')
|
||||||
|
|
||||||
|
class ExplicitPK(models.Model):
|
||||||
|
key = models.CharField(max_length=20, primary_key=True)
|
||||||
|
desc = models.CharField(max_length=20, blank=True, unique=True)
|
||||||
|
class Meta:
|
||||||
|
unique_together = ('key', 'desc')
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.key
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
__test__ = {'API_TESTS': """
|
||||||
>>> from django import forms
|
>>> from django import forms
|
||||||
>>> from django.forms.models import ModelForm, model_to_dict
|
>>> from django.forms.models import ModelForm, model_to_dict
|
||||||
|
@ -1247,6 +1256,27 @@ True
|
||||||
>>> form.is_valid()
|
>>> form.is_valid()
|
||||||
True
|
True
|
||||||
|
|
||||||
|
# Test for primary_key being in the form and failing validation.
|
||||||
|
>>> class ExplicitPKForm(ModelForm):
|
||||||
|
... class Meta:
|
||||||
|
... model = ExplicitPK
|
||||||
|
... fields = ('key', 'desc',)
|
||||||
|
>>> form = ExplicitPKForm({'key': u'', 'desc': u'' })
|
||||||
|
>>> form.is_valid()
|
||||||
|
False
|
||||||
|
|
||||||
|
# Ensure keys and blank character strings are tested for uniqueness.
|
||||||
|
>>> form = ExplicitPKForm({'key': u'key1', 'desc': u''})
|
||||||
|
>>> form.is_valid()
|
||||||
|
True
|
||||||
|
>>> form.save()
|
||||||
|
<ExplicitPK: key1>
|
||||||
|
>>> form = ExplicitPKForm({'key': u'key1', 'desc': u''})
|
||||||
|
>>> form.is_valid()
|
||||||
|
False
|
||||||
|
>>> form.errors
|
||||||
|
{'__all__': [u'Explicit pk with this Key and Desc already exists.'], 'key': [u'Explicit pk with this Key already exists.'], 'desc': [u'Explicit pk with this Desc already exists.']}
|
||||||
|
|
||||||
# Choices on CharField and IntegerField
|
# Choices on CharField and IntegerField
|
||||||
>>> class ArticleForm(ModelForm):
|
>>> class ArticleForm(ModelForm):
|
||||||
... class Meta:
|
... class Meta:
|
||||||
|
|
Loading…
Reference in New Issue