Ticket #14671: modelchoicefield_choices.diff

File modelchoicefield_choices.diff, 1.3 KB (added by Simon Litchfield, 14 years ago)
  • forms/models.py

     
    2222__all__ = (
    2323    'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model',
    2424    'save_instance', 'form_for_fields', 'ModelChoiceField',
    25     'ModelMultipleChoiceField',
     25    'ModelMultipleChoiceField', 'BaseModelFormSet', 'BaseInlineFormSet',
    2626)
    2727
    2828def construct_instance(form, instance, fields=None, exclude=None):
     
    10041004            return None
    10051005        try:
    10061006            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:
    10091015            raise ValidationError(self.error_messages['invalid_choice'])
    10101016        return value
    10111017
Back to Top