Ticket #4787: ModelChoiceField_update_choices_and_fix_clean_method.patch
File ModelChoiceField_update_choices_and_fix_clean_method.patch, 2.2 KB (added by , 17 years ago) |
---|
-
newforms/models.py
141 141 # actually use any of ChoiceField's implementation. 142 142 def __init__(self, queryset, empty_label=u"---------", cache_choices=False, 143 143 required=True, widget=Select, label=None, initial=None, help_text=None): 144 self. queryset = queryset144 self._queryset = queryset 145 145 self.empty_label = empty_label 146 146 self.cache_choices = cache_choices 147 147 # Call Field instead of ChoiceField __init__() because we don't need … … 149 149 Field.__init__(self, required, widget, label, initial, help_text) 150 150 self.widget.choices = self.choices 151 151 152 def _get_queryset(self): 153 return self._queryset 154 155 def _set_queryset(self, queryset): 156 self._queryset = queryset 157 self.widget.choices = self.choices 158 159 queryset = property(_get_queryset, _set_queryset) 160 152 161 def _get_choices(self): 153 162 # If self._choices is set, then somebody must have manually set 154 163 # the property self.choices. In this case, just return self._choices. … … 160 169 # *each* time _get_choices() is called (and, thus, each time 161 170 # self.choices is accessed) so that we can ensure the QuerySet has not 162 171 # been consumed. 163 return QuerySetIterator(self. queryset, self.empty_label, self.cache_choices)172 return QuerySetIterator(self._queryset, self.empty_label, self.cache_choices) 164 173 165 174 def _set_choices(self, value): 166 175 # This method is copied from ChoiceField._set_choices(). It's necessary … … 175 184 if value in ('', None): 176 185 return None 177 186 try: 178 value = self.queryset.model._default_manager.get(pk=value) 179 except self.queryset.model.DoesNotExist: 187 value = self._queryset.model._default_manager.get(pk=value) 188 if not value in self._queryset: 189 raise self._queryset.model.DoesNotExist 190 except self._queryset.model.DoesNotExist: 180 191 raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) 181 192 return value 182 193