Ticket #4046: formfield_patch.3.diff

File formfield_patch.3.diff, 1.5 KB (added by webograph <webograph@…>, 17 years ago)

svn upped to 5281 (didn't merge cleanly any more), i18n'ed exception. no changes as suggested by paul 05/16/07 yet.

  • django/newforms/models.py

     
    169169            return None
    170170        try:
    171171            value = self.queryset.model._default_manager.get(pk=value)
     172            if value not in self.queryset:
     173                    raise self.queryset.model.DoesNotExist(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
    172174        except self.queryset.model.DoesNotExist:
    173175            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
    174176        return value
  • django/db/models/fields/related.py

     
    553553        setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
    554554
    555555    def formfield(self, **kwargs):
    556         defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
     556        defaults = {'form_class': forms.ModelChoiceField}
     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)
     561
    557562        defaults.update(kwargs)
    558563        return super(ForeignKey, self).formfield(**defaults)
    559564
Back to Top