Ticket #16311: patch_list_filter_limit_to_related2.diff

File patch_list_filter_limit_to_related2.diff, 3.0 KB (added by Stan <stanislas.guerra@…>, 13 years ago)

Patch against trunk with a subclass of RelatedFieldListFilter.

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

     
    355355    def get_validator_unique_lookup_type(self):
    356356        return '%s__exact' % self.name
    357357
    358     def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
     358    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to=None):
    359359        """Returns choices with a default blank choices included, for use
    360360        as SelectField choices for this field."""
    361361        first_choice = include_blank and blank_choice or []
    362362        if self.choices:
    363363            return first_choice + list(self.choices)
    364364        rel_model = self.rel.to
     365        limit_choices_to = limit_choices_to and limit_choices_to or self.rel.limit_choices_to
    365366        if hasattr(self.rel, 'get_related_field'):
    366             lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     367            lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(limit_choices_to)]
    367368        else:
    368             lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     369            lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(limit_choices_to)]
    369370        return first_choice + lst
    370371
    371372    def get_choices_default(self):
  • django/contrib/admin/filters.py

     
    160160        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    161161        self.lookup_val_isnull = request.GET.get(
    162162                                      self.lookup_kwarg_isnull, None)
    163         self.lookup_choices = field.get_choices(include_blank=False)
     163        self.lookup_choices = self.field_choices(field, request, model_admin)
    164164        self.title = self.lookup_title
    165165
    166166    def has_output(self):
     
    175175    def used_params(self):
    176176        return [self.lookup_kwarg, self.lookup_kwarg_isnull]
    177177
     178    def field_choices(self, field, request, model_admin):
     179        return field.get_choices(include_blank=False)
     180
    178181    def choices(self, cl):
    179182        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
    180183        yield {
     
    399402            }
    400403
    401404FieldListFilter.register(lambda f: True, AllValuesFieldListFilter)
     405
     406
     407class RelatedOnlyFieldListFilter(RelatedFieldListFilter):
     408    def field_choices(self, field, request, model_admin):
     409        limit_choices_to = {'pk__in': set(model_admin.queryset(request).values_list(field.name, flat=True))}
     410        return field.get_choices(include_blank=False, limit_choices_to=limit_choices_to)
Back to Top