Ticket #10134: unique_for_stuff.2.diff
File unique_for_stuff.2.diff, 5.9 KB (added by , 16 years ago) |
---|
-
django/forms/models.py
diff --git a/django/forms/models.py b/django/forms/models.py index 7ca1ab5..2a9a1cc 100644
a b class BaseModelForm(BaseForm): 239 239 # not make sense to check data that didn't validate, and since NULL does not 240 240 # equal NULL in SQL we should not do any unique checking for NULL values. 241 241 unique_checks = [] 242 # these are checks for the unique_for_<date/year/month> 243 date_checks = [] 242 244 for check in self.instance._meta.unique_together[:]: 243 245 fields_on_form = [field for field in check if self.cleaned_data.get(field) is not None] 244 246 if len(fields_on_form) == len(check): … … class BaseModelForm(BaseForm): 260 262 # get_field_by_name found it, but it is not a Field so do not proceed 261 263 # to use it as if it were. 262 264 continue 263 if f.unique and self.cleaned_data.get(name) is not None: 265 if self.cleaned_data.get(name) is None: 266 continue 267 if f.unique: 264 268 unique_checks.append((name,)) 269 if f.unique_for_date and self.cleaned_data.get(f.unique_for_date) is not None: 270 date_checks.append(('date', name, f.unique_for_date)) 271 if f.unique_for_year and self.cleaned_data.get(f.unique_for_year) is not None: 272 date_checks.append(('year', name, f.unique_for_year)) 273 if f.unique_for_month and self.cleaned_data.get(f.unique_for_month) is not None: 274 date_checks.append(('month', name, f.unique_for_month)) 265 275 266 276 bad_fields = set() 267 277 for unique_check in unique_checks: … … class BaseModelForm(BaseForm): 315 325 for field_name in unique_check: 316 326 bad_fields.add(field_name) 317 327 328 for lookup_type, field, unique_for in date_checks: 329 lookup_kwargs = {} 330 # there's a ticket to add a date lookup, we can remove this special 331 # case if that makes it's way in 332 if lookup_type == 'date': 333 date = self.cleaned_data[unique_for] 334 lookup_kwargs['%s__day' % unique_for] = date.day 335 lookup_kwargs['%s__month' % unique_for] = date.month 336 lookup_kwargs['%s__year' % unique_for] = date.year 337 else: 338 lookup_kwargs['%s__%s' % (unique_for, lookup_type)] = getattr(self.cleaned_data[unique_for], lookup_type) 339 lookup_kwargs[field] = self.cleaned_data[field] 340 341 qs = self.instance.__class__._default_manager.filter(**lookup_kwargs) 342 # Exclude the current object from the query if we are editing an 343 # instance (as opposed to creating a new one) 344 if self.instance.pk is not None: 345 qs = qs.exclude(pk=self.instance.pk) 346 347 # This cute trick with extra/values is the most efficient way to 348 # tell if a particular query returns any results. 349 if qs.extra(select={'a': 1}).values('a').order_by(): 350 self._errors[field] = ErrorList([ 351 _(u"%(field_name)s must be unique for %(date_field)s %(lookup)s.") % { 352 'field_name': unicode(self.fields[field].label), 353 'date_field': unicode(self.fields[unique_for].label), 354 'lookup': lookup_type, 355 } 356 ]) 357 bad_fields.add(field) 358 318 359 for field_name in bad_fields: 319 360 del self.cleaned_data[field_name] 320 361 if form_errors: -
tests/modeltests/model_forms/models.py
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 992bb90..d5353ae 100644
a b class ExplicitPK(models.Model): 189 189 def __unicode__(self): 190 190 return self.key 191 191 192 class Post(models.Model): 193 title = models.CharField(max_length=50, unique_for_date='posted', blank=True) 194 slug = models.CharField(max_length=50, unique_for_year='posted', blank=True) 195 subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True) 196 posted = models.DateField() 197 198 def __unicode__(self): 199 return self.name 200 192 201 __test__ = {'API_TESTS': """ 193 202 >>> from django import forms 194 203 >>> from django.forms.models import ModelForm, model_to_dict … … ValidationError: [u'Select a valid choice. z is not one of the available choices 1472 1481 <tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description" /></td></tr> 1473 1482 <tr><th><label for="id_url">The URL:</label></th><td><input id="id_url" type="text" name="url" maxlength="40" /></td></tr> 1474 1483 1484 ### validation on unique_for_date 1485 1486 >>> p = Post.objects.create(title="Django 1.0 is released", slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3)) 1487 >>> class PostForm(ModelForm): 1488 ... class Meta: 1489 ... model = Post 1490 1491 >>> f = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'}) 1492 >>> f.is_valid() 1493 False 1494 >>> f.errors 1495 {'title': [u'Title must be unique for Posted date.']} 1496 >>> f = PostForm({'title': "Work on Django 1.1 begins", 'posted': '2008-09-03'}) 1497 >>> f.is_valid() 1498 True 1499 >>> f = PostForm({'title': "Django 1.0 is released", 'posted': '2008-09-04'}) 1500 >>> f.is_valid() 1501 True 1502 >>> f = PostForm({'slug': "Django 1.0", 'posted': '2008-01-01'}) 1503 >>> f.is_valid() 1504 False 1505 >>> f.errors 1506 {'slug': [u'Slug must be unique for Posted year.']} 1507 >>> f = PostForm({'subtitle': "Finally", 'posted': '2008-09-30'}) 1508 >>> f.is_valid() 1509 False 1510 >>> f.errors 1511 {'subtitle': [u'Subtitle must be unique for Posted month.']} 1512 >>> f = PostForm({'subtitle': "Finally", "title": "Django 1.0 is released", "slug": "Django 1.0", 'posted': '2008-09-03'}, instance=p) 1513 >>> f.is_valid() 1514 True 1515 1475 1516 # Clean up 1476 1517 >>> import shutil 1477 1518 >>> shutil.rmtree(temp_storage_dir)