Ticket #16311: patch_list_filter_limit_to_related.diff
File patch_list_filter_limit_to_related.diff, 2.5 KB (added by , 13 years ago) |
---|
-
django/db/models/fields/__init__.py
356 356 def get_validator_unique_lookup_type(self): 357 357 return '%s__exact' % self.name 358 358 359 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH ):359 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to={}): 360 360 """Returns choices with a default blank choices included, for use 361 361 as SelectField choices for this field.""" 362 362 first_choice = include_blank and blank_choice or [] 363 363 if self.choices: 364 364 return first_choice + list(self.choices) 365 365 rel_model = self.rel.to 366 if not limit_choices_to: 367 limit_choices_to = self.rel.limit_choices_to 366 368 if hasattr(self.rel, 'get_related_field'): 367 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)]369 lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(limit_choices_to)] 368 370 else: 369 lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter( self.rel.limit_choices_to)]371 lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(limit_choices_to)] 370 372 return first_choice + lst 371 373 372 374 def get_choices_default(self): -
django/contrib/admin/filterspecs.py
80 80 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 81 81 self.lookup_val_isnull = request.GET.get( 82 82 self.lookup_kwarg_isnull, None) 83 self.lookup_choices = f.get_choices(include_blank=False) 83 limit_choices_to = {} 84 if getattr(model_admin, '%s_filter_related_only' % f.name, False): 85 limit_choices_to['pk__in'] = set(model_admin.queryset(request).values_list(f.name, flat=True)) 86 self.lookup_choices = f.get_choices(include_blank=False, limit_choices_to=limit_choices_to) 84 87 85 88 def has_output(self): 86 89 if isinstance(self.field, models.related.RelatedObject) \