Ticket #16257: 16257_r16381.diff

File 16257_r16381.diff, 5.0 KB (added by graveyboat, 4 years ago)

Patch for the proposed change

  • docs/ref/contrib/admin/index.txt

     
    984984    displayed on the changelist view as described above in the
    985985    :attr:`ModelAdmin.list_display` section.
    986986
     987.. method:: ModelAdmin.get_list_display_links(self, request, list_display)
     988
     989    .. versionadded:: 1.4
     990
     991    The ``get_list_display_links`` method is given the ``HttpRequest`` and
     992    the ``list`` or ``tuple`` returned from ModelAdmin.get_list_display
     993    and is expected to return a ``list`` or ``tuple`` of field names that
     994    will be displayed on the changelist view as described above in the
     995    :attr:`ModelAdmin.list_display_links` section.
     996
    987997.. method:: ModelAdmin.get_urls(self)
    988998
    989999    The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for
  • django/contrib/admin/options.py

     
    632632        """
    633633        return self.list_display
    634634
     635    def get_list_display_links(self, request, list_display):
     636        """
     637        Return a sequence containing the fields to be displayed as links
     638        on the changelist.
     639        """
     640        return self.list_display_links
     641
    635642    def construct_change_message(self, request, form, formsets):
    636643        """
    637644        Construct a change message from a changed object.
     
    10691076
    10701077        ChangeList = self.get_changelist(request)
    10711078        try:
    1072             cl = ChangeList(request, self.model, list_display, self.list_display_links,
     1079            cl = ChangeList(request, self.model, list_display, list(self.get_list_display_links(request,list_display)),
    10731080                self.list_filter, self.date_hierarchy, self.search_fields,
    10741081                self.list_select_related, self.list_per_page, self.list_editable, self)
    10751082        except IncorrectLookupParameters:
  • tests/regressiontests/admin_changelist/tests.py

     
    351351        response.render()
    352352        self.assertContains(response, 'Parent object')
    353353
     354    def test_dynamic_list_display_links(self):
     355        """
     356        Regression tests for #16257: dynamic list_display_links support.
     357        """
     358        parent = Parent.objects.create(name='parent')
     359        for i in range(10):
     360            Child.objects.create(name='child %s' % i, parent=parent)
    354361
     362        user_noparents = User.objects.create(
     363            username='noparents',
     364            is_superuser=True)
     365        user_parents = User.objects.create(
     366            username='parents',
     367            is_superuser=True)
     368
     369        def _mocked_authenticated_request(user):
     370            request = self.factory.get('/child/')
     371            request.user = user
     372            return request
     373
     374        # Test with user 'noparents'
     375        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
     376        request = _mocked_authenticated_request(user_noparents)
     377        response = m.changelist_view(request)
     378        # XXX - Calling render here to avoid ContentNotRenderedError to be
     379        # raised. Ticket #15826 should fix this but it's not yet integrated.
     380        response.render()
     381        self.assertNotContains(response, 'Parent object')
     382
     383        # Test with user 'parents'
     384        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
     385        request = _mocked_authenticated_request(user_parents)
     386        response = m.changelist_view(request)
     387        # XXX - #15826
     388        response.render()
     389        self.assertContains(response, 'Parent object')
     390
     391        # Test default implementation
     392        m = ChildAdmin(Child, admin.site)
     393        request = _mocked_authenticated_request(user_noparents)
     394        response = m.changelist_view(request)
     395        # XXX - #15826
     396        response.render()
     397        self.assertContains(response, 'Parent object')
     398
     399
    355400class ParentAdmin(admin.ModelAdmin):
    356401    list_filter = ['child__name']
    357402    search_fields = ['child__name']
     
    405450            my_list_display.remove('parent')
    406451
    407452        return my_list_display
     453
     454class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):
     455    list_display = ('name', 'parent')
     456    list_display_links = ('parent',)
     457
     458    def get_list_display(self, request):
     459        my_list_display = list(self.list_display)
     460        if request.user.username == 'noparents':
     461            my_list_display.remove('parent')
     462
     463        return my_list_display
     464
     465    def get_list_display_links(self, request, list_display):
     466        my_list_display_links = list(set(list_display) & set(self.list_display_links))
     467        if my_list_display_links == []:
     468            my_list_display_links = [list_display[1]]
     469
     470        return my_list_display_links
Back to Top