Author: jacob
Date: 2008-09-02 09:20:11 -0500 (Tue, 02 Sep 2008)
New Revision: 8854

Modified:
   django/trunk/django/forms/models.py
   django/trunk/tests/modeltests/model_forms/models.py
Log:
Fixed #8795: unique_together validation no longer fails on model forms that 
exclude fields included in the check. Thanks, Alex Gaynor.

Modified: django/trunk/django/forms/models.py
===================================================================
--- django/trunk/django/forms/models.py 2008-09-02 13:52:07 UTC (rev 8853)
+++ django/trunk/django/forms/models.py 2008-09-02 14:20:11 UTC (rev 8854)
@@ -210,10 +210,20 @@
 
     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]

Modified: django/trunk/tests/modeltests/model_forms/models.py
===================================================================
--- django/trunk/tests/modeltests/model_forms/models.py 2008-09-02 13:52:07 UTC 
(rev 8853)
+++ django/trunk/tests/modeltests/model_forms/models.py 2008-09-02 14:20:11 UTC 
(rev 8854)
@@ -1192,6 +1192,14 @@
 >>> 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:


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to