Django

Code

Changeset 9341

Show
Ignore:
Timestamp:
11/05/08 13:47:44 (2 months ago)
Author:
kmtracey
Message:

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).

Files:

Legend:

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

    r9334 r9341  
    235235        unique_checks = [] 
    236236        for check in self.instance._meta.unique_together[:]: 
    237             fields_on_form = [field for field in check if field in self.cleaned_data and not self.cleaned_data[field] is None] 
     237            fields_on_form = [field for field in check if self.cleaned_data.get(field) is not None] 
    238238            if len(fields_on_form) == len(check): 
    239239                unique_checks.append(check) 
     
    249249                # This is an extra field that's not on the ModelForm, ignore it 
    250250                continue 
    251             if f.unique and name in self.cleaned_data and not self.cleaned_data[name] is None: 
     251            if f.unique and self.cleaned_data.get(name) is not None: 
    252252                unique_checks.append((name,)) 
    253253 
  • django/trunk/tests/modeltests/model_forms/models.py

    r9334 r9341  
    159159    class Meta: 
    160160        unique_together = ('title', 'author') 
     161         
     162class ExplicitPK(models.Model): 
     163    key = models.CharField(max_length=20, primary_key=True) 
     164    desc = models.CharField(max_length=20, blank=True, unique=True) 
     165    class Meta: 
     166        unique_together = ('key', 'desc') 
     167     
     168    def __unicode__(self): 
     169        return self.key 
    161170 
    162171__test__ = {'API_TESTS': """ 
     
    12481257True 
    12491258 
     1259# Test for primary_key being in the form and failing validation. 
     1260>>> class ExplicitPKForm(ModelForm): 
     1261...     class Meta: 
     1262...         model = ExplicitPK 
     1263...         fields = ('key', 'desc',) 
     1264>>> form = ExplicitPKForm({'key': u'', 'desc': u'' }) 
     1265>>> form.is_valid() 
     1266False 
     1267 
     1268# Ensure keys and blank character strings are tested for uniqueness. 
     1269>>> form = ExplicitPKForm({'key': u'key1', 'desc': u''}) 
     1270>>> form.is_valid() 
     1271True 
     1272>>> form.save() 
     1273<ExplicitPK: key1> 
     1274>>> form = ExplicitPKForm({'key': u'key1', 'desc': u''}) 
     1275>>> form.is_valid() 
     1276False 
     1277>>> form.errors 
     1278{'__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.']} 
     1279 
    12501280# Choices on CharField and IntegerField 
    12511281>>> class ArticleForm(ModelForm):