Changeset 6670
- Timestamp:
- 11/13/07 08:36:29 (8 months ago)
- Files:
-
- django/trunk/django/newforms/models.py (modified) (4 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/models.py
r6668 r6670 156 156 required=True, widget=Select, label=None, initial=None, 157 157 help_text=None): 158 self.queryset = queryset159 158 self.empty_label = empty_label 160 159 self.cache_choices = cache_choices … … 162 161 # ChoiceField.__init__(). 163 162 Field.__init__(self, required, widget, label, initial, help_text) 163 self.queryset = queryset 164 165 def _get_queryset(self): 166 return self._queryset 167 168 def _set_queryset(self, queryset): 169 self._queryset = queryset 164 170 self.widget.choices = self.choices 171 172 queryset = property(_get_queryset, _set_queryset) 165 173 166 174 def _get_choices(self): … … 191 199 return None 192 200 try: 193 value = self.queryset. model._default_manager.get(pk=value)201 value = self.queryset.get(pk=value) 194 202 except self.queryset.model.DoesNotExist: 195 203 raise ValidationError(ugettext(u'Select a valid choice. That' … … 218 226 for val in value: 219 227 try: 220 obj = self.queryset. model._default_manager.get(pk=val)228 obj = self.queryset.get(pk=val) 221 229 except self.queryset.model.DoesNotExist: 222 230 raise ValidationError(ugettext(u'Select a valid choice. %s is' django/trunk/tests/modeltests/model_forms/models.py
r6660 r6670 441 441 442 442 >>> f = ModelChoiceField(Category.objects.all()) 443 >>> list(f.choices) 444 [(u'', u'---------'), (1, u'Entertainment'), (2, u"It's a test"), (3, u'Third'), (4, u'Fourth')] 443 445 >>> f.clean('') 444 446 Traceback (most recent call last): … … 486 488 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 487 489 490 # queryset can be changed after the field is created. 491 >>> f.queryset = Category.objects.exclude(name='Fourth') 492 >>> list(f.choices) 493 [(u'', u'---------'), (1, u'Entertainment'), (2, u"It's a test"), (3, u'Third')] 494 >>> f.clean(3) 495 <Category: Third> 496 >>> f.clean(4) 497 Traceback (most recent call last): 498 ... 499 ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 500 501 488 502 # ModelMultipleChoiceField #################################################### 489 503 490 504 >>> f = ModelMultipleChoiceField(Category.objects.all()) 505 >>> list(f.choices) 506 [(1, u'Entertainment'), (2, u"It's a test"), (3, u'Third'), (4, u'Fourth')] 491 507 >>> f.clean(None) 492 508 Traceback (most recent call last): … … 553 569 ValidationError: [u'Select a valid choice. 10 is not one of the available choices.'] 554 570 571 # queryset can be changed after the field is created. 572 >>> f.queryset = Category.objects.exclude(name='Fourth') 573 >>> list(f.choices) 574 [(1, u'Entertainment'), (2, u"It's a test"), (3, u'Third')] 575 >>> f.clean([3]) 576 [<Category: Third>] 577 >>> f.clean([4]) 578 Traceback (most recent call last): 579 ... 580 ValidationError: [u'Select a valid choice. 4 is not one of the available choices.'] 581 >>> f.clean(['3', '4']) 582 Traceback (most recent call last): 583 ... 584 ValidationError: [u'Select a valid choice. 4 is not one of the available choices.'] 585 586 555 587 # PhoneNumberField ############################################################ 556 588
