Ticket #4046: formfield_patch.2.diff

File formfield_patch.2.diff, 1.6 KB (added by webograph <webograph@…>, 8 years ago)

updated patch that also changes newforms' model.py to respect the limited choices in clean()

  • django/db/models/fields/related.py

     
    553553        setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
    554554
    555555    def formfield(self, **kwargs):
    556         defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     556        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     557        if hasattr(self.rel.limit_choices_to, 'get_sql'):
     558             defaults['queryset']=self.rel.to._default_manager.filter(self.rel.limit_choices_to)
     559        else:
     560             defaults['queryset']=self.rel.to._default_manager.filter(**self.rel.limit_choices_to)
    557561        defaults.update(kwargs)
    558562        return forms.ModelChoiceField(**defaults)
    559563
  • django/newforms/models.py

     
    161161            return None
    162162        try:
    163163            value = self.queryset.model._default_manager.get(pk=value)
     164            if value not in self.queryset: raise self.queryset.model.DoesNotExist, "The specified object exists but is not among the choices."
    164165        except self.queryset.model.DoesNotExist:
    165166            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
    166167        return value
Back to Top