summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-09-02 14:20:11 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-09-02 14:20:11 +0000
commit98e1cc92f47cbda977a923b9ba8d980bc01cd749 (patch)
treea3e779853a15cb1d987d4c39082a8cbdb3ae49c1
parent5c32fe7fad5c52fb5b62dc97503aed516bb9c668 (diff)
Fixed #8795: unique_together validation no longer fails on model forms that exclude fields included in the check. Thanks, Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8854 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/models.py14
-rw-r--r--tests/modeltests/model_forms/models.py8
2 files changed, 20 insertions, 2 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 56e7f2aabd..cd77a90248 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -210,10 +210,20 @@ class BaseModelForm(BaseForm):
def validate_unique(self):
from django.db.models.fields import FieldDoesNotExist
- unique_checks = list(self.instance._meta.unique_together[:])
+
+ # Gather a list of checks to perform. Since this is a ModelForm, some
+ # fields may have been excluded; we can't perform a unique check on a
+ # form that is missing fields involved in that check.
+ unique_checks = []
+ for check in self.instance._meta.unique_together[:]:
+ fields_on_form = [field for field in check if field in self.fields]
+ if len(fields_on_form) == len(check):
+ unique_checks.append(check)
+
form_errors = []
- # Make sure the unique checks apply to actual fields on the ModelForm
+ # Gather a list of checks for fields declared as unique and add them to
+ # the list of checks. Again, skip fields not on the form.
for name, field in self.fields.items():
try:
f = self.instance._meta.get_field_by_name(name)[0]
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index dd6e25f716..e3821de758 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -1192,6 +1192,14 @@ False
>>> form._errors
{'__all__': [u'Price with this Price and Quantity already exists.']}
+>>> class PriceForm(ModelForm):
+... class Meta:
+... model = Price
+... exclude = ('quantity',)
+>>> form = PriceForm({'price': '6.00'})
+>>> form.is_valid()
+True
+
# Choices on CharField and IntegerField
>>> class ArticleForm(ModelForm):
... class Meta: