Django

Code

Changeset 6670

Show
Ignore:
Timestamp:
11/13/07 08:36:29 (8 months ago)
Author:
gwilson
Message:

Fixed #4787, #5913 -- Updating the queryset on a ModelChoiceField or ModelMultipleChoiceField now updates its widget's choices. The clean methods for ModelChoiceField and ModelMultipleChoiceField were changed to only allow choices in the specified queryset (instead of allowing all choices returned by the queryset model's default manager).

Files:

Legend:

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

    r6668 r6670  
    156156                 required=True, widget=Select, label=None, initial=None, 
    157157                 help_text=None): 
    158         self.queryset = queryset 
    159158        self.empty_label = empty_label 
    160159        self.cache_choices = cache_choices 
     
    162161        # ChoiceField.__init__(). 
    163162        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 
    164170        self.widget.choices = self.choices 
     171 
     172    queryset = property(_get_queryset, _set_queryset) 
    165173 
    166174    def _get_choices(self): 
     
    191199            return None 
    192200        try: 
    193             value = self.queryset.model._default_manager.get(pk=value) 
     201            value = self.queryset.get(pk=value) 
    194202        except self.queryset.model.DoesNotExist: 
    195203            raise ValidationError(ugettext(u'Select a valid choice. That' 
     
    218226        for val in value: 
    219227            try: 
    220                 obj = self.queryset.model._default_manager.get(pk=val) 
     228                obj = self.queryset.get(pk=val) 
    221229            except self.queryset.model.DoesNotExist: 
    222230                raise ValidationError(ugettext(u'Select a valid choice. %s is' 
  • django/trunk/tests/modeltests/model_forms/models.py

    r6660 r6670  
    441441 
    442442>>> 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')] 
    443445>>> f.clean('') 
    444446Traceback (most recent call last): 
     
    486488ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 
    487489 
     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) 
     497Traceback (most recent call last): 
     498... 
     499ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] 
     500 
     501 
    488502# ModelMultipleChoiceField #################################################### 
    489503 
    490504>>> 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')] 
    491507>>> f.clean(None) 
    492508Traceback (most recent call last): 
     
    553569ValidationError: [u'Select a valid choice. 10 is not one of the available choices.'] 
    554570 
     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]) 
     578Traceback (most recent call last): 
     579... 
     580ValidationError: [u'Select a valid choice. 4 is not one of the available choices.'] 
     581>>> f.clean(['3', '4']) 
     582Traceback (most recent call last): 
     583... 
     584ValidationError: [u'Select a valid choice. 4 is not one of the available choices.'] 
     585 
     586 
    555587# PhoneNumberField ############################################################ 
    556588