Ticket #16257: 16257_r16400.diff
File 16257_r16400.diff, 5.1 KB (added by , 13 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
1005 1005 displayed on the changelist view as described above in the 1006 1006 :attr:`ModelAdmin.list_display` section. 1007 1007 1008 .. method:: ModelAdmin.get_list_display_links(self, request, list_display) 1009 1010 .. versionadded:: 1.4 1011 1012 The ``get_list_display_links`` method is given the ``HttpRequest`` and 1013 the ``list`` or ``tuple`` returned from ModelAdmin.get_list_display 1014 and is expected to return a ``list`` or ``tuple`` of field names that 1015 will be displayed on the changelist view as described above in the 1016 :attr:`ModelAdmin.list_display_links` section. 1017 1008 1018 .. method:: ModelAdmin.get_urls(self) 1009 1019 1010 1020 The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for -
tests/regressiontests/admin_changelist/tests.py
351 351 response.render() 352 352 self.assertContains(response, 'Parent object') 353 353 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) 354 361 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 355 400 class ParentAdmin(admin.ModelAdmin): 356 401 list_filter = ['child__name'] 357 402 search_fields = ['child__name'] … … 405 450 my_list_display.remove('parent') 406 451 407 452 return my_list_display 453 454 class 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 -
django/contrib/admin/options.py
639 639 """ 640 640 return self.list_display 641 641 642 def get_list_display_links(self, request, list_display): 643 """ 644 Return a sequence containing the fields to be displayed as links 645 on the changelist. 646 """ 647 return self.list_display_links 648 642 649 def construct_change_message(self, request, form, formsets): 643 650 """ 644 651 Construct a change message from a changed object. … … 1073 1080 list_display.remove('action_checkbox') 1074 1081 except ValueError: 1075 1082 pass 1083 list_display_links = list(self.get_list_display_links(request,list_display)) 1076 1084 1077 1085 ChangeList = self.get_changelist(request) 1078 1086 try: 1079 cl = ChangeList(request, self.model, list_display, self.list_display_links,1087 cl = ChangeList(request, self.model, list_display, list_display_links), 1080 1088 self.list_filter, self.date_hierarchy, self.search_fields, 1081 1089 self.list_select_related, self.list_per_page, self.list_editable, self) 1082 1090 except IncorrectLookupParameters: