Ticket #14671: modelchoicefield_choices.diff
File modelchoicefield_choices.diff, 1.3 KB (added by , 14 years ago) |
---|
-
forms/models.py
22 22 __all__ = ( 23 23 'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model', 24 24 'save_instance', 'form_for_fields', 'ModelChoiceField', 25 'ModelMultipleChoiceField', 25 'ModelMultipleChoiceField', 'BaseModelFormSet', 'BaseInlineFormSet', 26 26 ) 27 27 28 28 def construct_instance(form, instance, fields=None, exclude=None): … … 1004 1004 return None 1005 1005 try: 1006 1006 key = self.to_field_name or 'pk' 1007 value = self.queryset.get(**{key: value}) 1008 except (ValueError, self.queryset.model.DoesNotExist): 1007 if hasattr(self, '_choices'): 1008 # If self._choices is set, then it was set manually so we need to 1009 # check it rather than the queryset 1010 assert smart_unicode(value) in [ smart_unicode(pk) for pk, label in self._choices ] 1011 value = self.queryset.model.objects.get(**{key: value}) 1012 else: 1013 value = self.queryset.get(**{key: value}) 1014 except (AssertionError, ValueError, self.queryset.model.DoesNotExist), e: 1009 1015 raise ValidationError(self.error_messages['invalid_choice']) 1010 1016 return value 1011 1017