Django

Code

Changeset 8854

Show
Ignore:
Timestamp:
09/02/08 09:20:11 (3 months ago)
Author:
jacob
Message:

Fixed #8795: unique_together validation no longer fails on model forms that exclude fields included in the check. Thanks, Alex Gaynor.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/models.py

    r8823 r8854  
    211211    def validate_unique(self): 
    212212        from django.db.models.fields import FieldDoesNotExist 
    213         unique_checks = list(self.instance._meta.unique_together[:]) 
     213 
     214        # Gather a list of checks to perform. Since this is a ModelForm, some 
     215        # fields may have been excluded; we can't perform a unique check on a 
     216        # form that is missing fields involved in that check. 
     217        unique_checks = [] 
     218        for check in self.instance._meta.unique_together[:]: 
     219            fields_on_form = [field for field in check if field in self.fields] 
     220            if len(fields_on_form) == len(check): 
     221                unique_checks.append(check) 
     222             
    214223        form_errors = [] 
    215224         
    216         # Make sure the unique checks apply to actual fields on the ModelForm 
     225        # Gather a list of checks for fields declared as unique and add them to 
     226        # the list of checks. Again, skip fields not on the form. 
    217227        for name, field in self.fields.items(): 
    218228            try: 
  • django/trunk/tests/modeltests/model_forms/models.py

    r8842 r8854  
    11931193{'__all__': [u'Price with this Price and Quantity already exists.']} 
    11941194 
     1195>>> class PriceForm(ModelForm): 
     1196...     class Meta: 
     1197...         model = Price 
     1198...         exclude = ('quantity',) 
     1199>>> form = PriceForm({'price': '6.00'}) 
     1200>>> form.is_valid() 
     1201True 
     1202 
    11951203# Choices on CharField and IntegerField 
    11961204>>> class ArticleForm(ModelForm):