Ticket #17646: ticket17646.diff
File ticket17646.diff, 6.1 KB (added by , 12 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 2071792..0fe6bc4 100644
a b class ModelAdmin(BaseModelAdmin): 662 662 # Use only the first item in list_display as link 663 663 return list(list_display)[:1] 664 664 665 def get_list_filter(self, request): 666 """ 667 Returns a sequence containing the fields to be displayed as filters in 668 the right sidebar of the changelist page. 669 """ 670 return self.list_filter 671 665 672 def construct_change_message(self, request, form, formsets): 666 673 """ 667 674 Construct a change message from a changed object. … … class ModelAdmin(BaseModelAdmin): 1112 1119 1113 1120 list_display = self.get_list_display(request) 1114 1121 list_display_links = self.get_list_display_links(request, list_display) 1122 list_filter = self.get_list_filter(request) 1115 1123 1116 1124 # Check actions to see if any are available on this changelist 1117 1125 actions = self.get_actions(request) … … class ModelAdmin(BaseModelAdmin): 1122 1130 ChangeList = self.get_changelist(request) 1123 1131 try: 1124 1132 cl = ChangeList(request, self.model, list_display, 1125 list_display_links, self.list_filter, self.date_hierarchy,1133 list_display_links, list_filter, self.date_hierarchy, 1126 1134 self.search_fields, self.list_select_related, 1127 1135 self.list_per_page, self.list_max_show_all, self.list_editable, 1128 1136 self) -
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index f3e39b9..03a07ed 100644
a b templates used by the :class:`ModelAdmin` views: 1070 1070 changelist that will be linked to the change view, as described in the 1071 1071 :attr:`ModelAdmin.list_display_links` section. 1072 1072 1073 .. method:: ModelAdmin.get_list_filter(self, request) 1074 1075 .. versionadded:: 1.5 1076 1077 The ``get_list_filter`` is given the ``HttpRequest`` and is expected to 1078 return a ``list`` or ``tuple`` of field names that will be used as filters 1079 in the right sidebar of the changelist page. 1080 1073 1081 .. method:: ModelAdmin.get_urls(self) 1074 1082 1075 1083 The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for -
docs/releases/1.5.txt
diff --git a/docs/releases/1.5.txt b/docs/releases/1.5.txt index 51e64bd..ef93a78 100644
a b Django 1.5 also includes several smaller improvements worth noting: 60 60 * In the localflavor for Canada, "pq" was added to the acceptable codes for 61 61 Quebec. It's an old abbreviation. 62 62 63 * The filters in the changelist page of the admin interface can now be customized 64 dependind on the request's context, by overriding the ModelAdmin.get_list_filter(self, request) 65 method. 66 63 67 Backwards incompatible changes in 1.5 64 68 ===================================== 65 69 -
tests/regressiontests/admin_changelist/admin.py
diff --git a/tests/regressiontests/admin_changelist/admin.py b/tests/regressiontests/admin_changelist/admin.py index 9ecfbc6..5751d04 100644
a b class ParentAdmin(admin.ModelAdmin): 32 32 class ChildAdmin(admin.ModelAdmin): 33 33 list_display = ['name', 'parent'] 34 34 list_per_page = 10 35 list_filter = ['parent', 'age'] 35 36 36 37 def queryset(self, request): 37 38 return super(ChildAdmin, self).queryset(request).select_related("parent__name") … … class SwallowAdmin(admin.ModelAdmin): 90 91 list_display = ('origin', 'load', 'speed') 91 92 92 93 site.register(Swallow, SwallowAdmin) 94 95 class DynamicListFilterChildAdmin(admin.ModelAdmin): 96 list_filter = ('parent', 'name', 'age') 97 98 def get_list_filter(self, request): 99 my_list_filter = super(DynamicListFilterChildAdmin, self).get_list_filter(request) 100 if request.user.username == 'noparents': 101 my_list_filter = list(my_list_filter) 102 my_list_filter.remove('parent') 103 return my_list_filter 104 -
tests/regressiontests/admin_changelist/tests.py
diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py index 62166ce..99a01d2 100644
a b from .admin import (ChildAdmin, QuartetAdmin, BandAdmin, ChordsBandAdmin, 15 15 GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin, 16 16 DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin, 17 17 FilteredChildAdmin, CustomPaginator, site as custom_site, 18 SwallowAdmin )18 SwallowAdmin, DynamicListFilterChildAdmin) 19 19 from .models import (Event, Child, Parent, Genre, Band, Musician, Group, 20 20 Quartet, Membership, ChordsMusician, ChordsBand, Invitation, Swallow, 21 21 UnorderedObject, OrderedObject) … … class ChangeListTests(TestCase): 536 536 check_results_order() 537 537 OrderedObjectAdmin.ordering = ['id', 'bool'] 538 538 check_results_order(ascending=True) 539 540 def test_dynamic_list_filter(self): 541 """ 542 Regression tests for ticket #17646: dynamic list_filter support. 543 """ 544 parent = Parent.objects.create(name='parent') 545 for i in range(10): 546 Child.objects.create(name='child %s' % i, parent=parent) 547 548 user_noparents = self._create_superuser('noparents') 549 user_parents = self._create_superuser('parents') 550 551 # Test with user 'noparents' 552 m = DynamicListFilterChildAdmin(Child, admin.site) 553 request = self._mocked_authenticated_request('/child/', user_noparents) 554 response = m.changelist_view(request) 555 556 list_filter = m.get_list_filter(request) 557 self.assertEqual(list_filter, ['name', 'age']) 558 559 # Test with user 'parents' 560 m = DynamicListFilterChildAdmin(Child, admin.site) 561 request = self._mocked_authenticated_request('/child/', user_parents) 562 response = m.changelist_view(request) 563 564 list_filter = m.get_list_filter(request) 565 self.assertEqual(list_filter, ('parent', 'name', 'age')) 566