Ticket #16311: patch_list_filter_limit_to_related2.diff
File patch_list_filter_limit_to_related2.diff, 3.0 KB (added by , 13 years ago) |
---|
-
django/db/models/fields/__init__.py
355 355 def get_validator_unique_lookup_type(self): 356 356 return '%s__exact' % self.name 357 357 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): 359 359 """Returns choices with a default blank choices included, for use 360 360 as SelectField choices for this field.""" 361 361 first_choice = include_blank and blank_choice or [] 362 362 if self.choices: 363 363 return first_choice + list(self.choices) 364 364 rel_model = self.rel.to 365 limit_choices_to = limit_choices_to and limit_choices_to or self.rel.limit_choices_to 365 366 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)] 367 368 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)] 369 370 return first_choice + lst 370 371 371 372 def get_choices_default(self): -
django/contrib/admin/filters.py
160 160 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 161 161 self.lookup_val_isnull = request.GET.get( 162 162 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) 164 164 self.title = self.lookup_title 165 165 166 166 def has_output(self): … … 175 175 def used_params(self): 176 176 return [self.lookup_kwarg, self.lookup_kwarg_isnull] 177 177 178 def field_choices(self, field, request, model_admin): 179 return field.get_choices(include_blank=False) 180 178 181 def choices(self, cl): 179 182 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE 180 183 yield { … … 399 402 } 400 403 401 404 FieldListFilter.register(lambda f: True, AllValuesFieldListFilter) 405 406 407 class 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)