Ticket #16311: patch16311-1.4.1.diff
File patch16311-1.4.1.diff, 7.0 KB (added by , 12 years ago) |
---|
-
django/contrib/admin/filters.py
diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index 76b8d30..f33cde4 100644
a b class RelatedFieldListFilter(FieldListFilter): 164 164 self.lookup_val = request.GET.get(self.lookup_kwarg, None) 165 165 self.lookup_val_isnull = request.GET.get( 166 166 self.lookup_kwarg_isnull, None) 167 self.lookup_choices = field.get_choices(include_blank=False)167 self.lookup_choices = self.field_choices(field, request, model_admin) 168 168 super(RelatedFieldListFilter, self).__init__( 169 169 field, request, params, model, model_admin, field_path) 170 170 if hasattr(field, 'verbose_name'): … … class RelatedFieldListFilter(FieldListFilter): 185 185 def expected_parameters(self): 186 186 return [self.lookup_kwarg, self.lookup_kwarg_isnull] 187 187 188 def field_choices(self, field, request, model_admin): 189 return field.get_choices(include_blank=False) 190 188 191 def choices(self, cl): 189 192 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE 190 193 yield { … … class AllValuesFieldListFilter(FieldListFilter): 403 406 } 404 407 405 408 FieldListFilter.register(lambda f: True, AllValuesFieldListFilter) 409 410 411 class RelatedOnlyFieldListFilter(RelatedFieldListFilter): 412 def field_choices(self, field, request, model_admin): 413 limit_choices_to = {'pk__in': set(model_admin.queryset(request).values_list(field.name, flat=True))} 414 return field.get_choices(include_blank=False, limit_choices_to=limit_choices_to) -
django/db/models/fields/__init__.py
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 22546c2..db2c5bf 100644
a b class Field(object): 386 386 def get_validator_unique_lookup_type(self): 387 387 return '%s__exact' % self.name 388 388 389 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH ):389 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to=None): 390 390 """Returns choices with a default blank choices included, for use 391 391 as SelectField choices for this field.""" 392 392 first_choice = include_blank and blank_choice or [] 393 393 if self.choices: 394 394 return first_choice + list(self.choices) 395 395 rel_model = self.rel.to 396 limit_choices_to = limit_choices_to and limit_choices_to or self.rel.limit_choices_to 396 397 if hasattr(self.rel, 'get_related_field'): 397 398 lst = [(getattr(x, self.rel.get_related_field().attname), 398 399 smart_unicode(x)) 399 400 for x in rel_model._default_manager.complex_filter( 400 self.rel.limit_choices_to)]401 limit_choices_to)] 401 402 else: 402 lst = [(x._get_pk_val(), smart_unicode(x)) 403 lst = [(x._get_pk_val(), smart_unicode(x)) 403 404 for x in rel_model._default_manager.complex_filter( 404 self.rel.limit_choices_to)]405 limit_choices_to)] 405 406 return first_choice + lst 406 407 407 408 def get_choices_default(self): -
tests/regressiontests/admin_filters/tests.py
diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index e2a12c9..178e3cd 100644
a b from django.contrib.admin import (site, ModelAdmin, SimpleListFilter, 7 7 from django.contrib.admin.views.main import ChangeList 8 8 from django.contrib.auth.admin import UserAdmin 9 9 from django.contrib.auth.models import User 10 from django.contrib.admin.filters import RelatedOnlyFieldListFilter 10 11 from django.core.exceptions import ImproperlyConfigured 11 12 from django.test import TestCase, RequestFactory 12 13 from django.test.utils import override_settings … … class BookAdmin(ModelAdmin): 87 88 class BookAdminWithTupleBooleanFilter(BookAdmin): 88 89 list_filter = ('year', 'author', 'contributors', ('is_best_seller', BooleanFieldListFilter), 'date_registered', 'no') 89 90 91 class BookAdminRelatedOnlyFilter(ModelAdmin): 92 list_filter = ('year', ('author', RelatedOnlyFieldListFilter), ('contributors', RelatedOnlyFieldListFilter),\ 93 'is_best_seller', 'date_registered', 'no') 94 ordering = ('-id',) 95 90 96 class DecadeFilterBookAdmin(ModelAdmin): 91 97 list_filter = ('author', DecadeListFilterWithTitleAndParameter) 92 98 ordering = ('-id',) … … class ListFiltersTests(TestCase): 261 267 def test_relatedfieldlistfilter_foreignkey(self): 262 268 modeladmin = BookAdmin(Book, site) 263 269 270 request = self.request_factory.get('/') 271 changelist = self.get_changelist(request, Book, modeladmin) 272 273 # Make sure that all users are present in the author's list filter 274 filterspec = changelist.get_filters(request)[0][1] 275 self.assertEqual(filterspec.lookup_choices, [(1, u'alfred'), (2, u'bob'), (3, u'lisa')]) 276 264 277 request = self.request_factory.get('/', {'author__isnull': 'True'}) 265 278 changelist = self.get_changelist(request, Book, modeladmin) 266 279 … … class ListFiltersTests(TestCase): 289 302 def test_relatedfieldlistfilter_manytomany(self): 290 303 modeladmin = BookAdmin(Book, site) 291 304 305 request = self.request_factory.get('/') 306 changelist = self.get_changelist(request, Book, modeladmin) 307 308 # Make sure that all users are present in the contrib's list filter 309 filterspec = changelist.get_filters(request)[0][2] 310 self.assertEqual(filterspec.lookup_choices, [(1, u'alfred'), (2, u'bob'), (3, u'lisa')]) 311 292 312 request = self.request_factory.get('/', {'contributors__isnull': 'True'}) 293 313 changelist = self.get_changelist(request, Book, modeladmin) 294 314 … … class ListFiltersTests(TestCase): 366 386 self.assertEqual(choice['selected'], True) 367 387 self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk) 368 388 389 def test_relatedonlyfieldlistfilter_foreignkey(self): 390 modeladmin = BookAdminRelatedOnlyFilter(Book, site) 391 392 request = self.request_factory.get('/') 393 changelist = self.get_changelist(request, Book, modeladmin) 394 395 # Make sure that only actual authors are present in author's list filter 396 filterspec = changelist.get_filters(request)[0][1] 397 self.assertEqual(filterspec.lookup_choices, [(1, u'alfred'), (2, u'bob')]) 398 399 def test_relatedonlyfieldlistfilter_manytomany(self): 400 modeladmin = BookAdminRelatedOnlyFilter(Book, site) 401 402 request = self.request_factory.get('/') 403 changelist = self.get_changelist(request, Book, modeladmin) 404 405 # Make sure that only actual contributors are present in contrib's list filter 406 filterspec = changelist.get_filters(request)[0][2] 407 self.assertEqual(filterspec.lookup_choices, [(2, u'bob'), (3, u'lisa')]) 408 369 409 def test_booleanfieldlistfilter(self): 370 410 modeladmin = BookAdmin(Book, site) 371 411 self.verify_booleanfieldlistfilter(modeladmin)