Ticket #17646: ticket17646.diff

File ticket17646.diff, 6.1 KB (added by mateusgondim, 3 years ago)

I created the hook there in options.py and replaced it in another method that was using the property directly.i also added a doc about the new method in the index.txt of admin, and in the section of minor features in release notes.Finally, i added a test in regression tests about that.

  • 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): 
    662662            # Use only the first item in list_display as link
    663663            return list(list_display)[:1]
    664664
     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
    665672    def construct_change_message(self, request, form, formsets):
    666673        """
    667674        Construct a change message from a changed object.
    class ModelAdmin(BaseModelAdmin): 
    11121119
    11131120        list_display = self.get_list_display(request)
    11141121        list_display_links = self.get_list_display_links(request, list_display)
     1122        list_filter = self.get_list_filter(request)
    11151123
    11161124        # Check actions to see if any are available on this changelist
    11171125        actions = self.get_actions(request)
    class ModelAdmin(BaseModelAdmin): 
    11221130        ChangeList = self.get_changelist(request)
    11231131        try:
    11241132            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,
    11261134                self.search_fields, self.list_select_related,
    11271135                self.list_per_page, self.list_max_show_all, self.list_editable,
    11281136                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: 
    10701070    changelist that will be linked to the change view, as described in the
    10711071    :attr:`ModelAdmin.list_display_links` section.
    10721072
     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
    10731081.. method:: ModelAdmin.get_urls(self)
    10741082
    10751083    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: 
    6060* In the localflavor for Canada, "pq" was added to the acceptable codes for
    6161  Quebec. It's an old abbreviation.
    6262
     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
    6367Backwards incompatible changes in 1.5
    6468=====================================
    6569
  • 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): 
    3232class ChildAdmin(admin.ModelAdmin):
    3333    list_display = ['name', 'parent']
    3434    list_per_page = 10
     35    list_filter = ['parent', 'age']
    3536
    3637    def queryset(self, request):
    3738        return super(ChildAdmin, self).queryset(request).select_related("parent__name")
    class SwallowAdmin(admin.ModelAdmin): 
    9091    list_display = ('origin', 'load', 'speed')
    9192
    9293site.register(Swallow, SwallowAdmin)
     94
     95class 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, 
    1515    GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin,
    1616    DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin,
    1717    FilteredChildAdmin, CustomPaginator, site as custom_site,
    18     SwallowAdmin)
     18    SwallowAdmin, DynamicListFilterChildAdmin)
    1919from .models import (Event, Child, Parent, Genre, Band, Musician, Group,
    2020    Quartet, Membership, ChordsMusician, ChordsBand, Invitation, Swallow,
    2121    UnorderedObject, OrderedObject)
    class ChangeListTests(TestCase): 
    536536        check_results_order()
    537537        OrderedObjectAdmin.ordering = ['id', 'bool']
    538538        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
Back to Top