Ticket #14206: 14206_r16204.diff
File 14206_r16204.diff, 4.7 KB (added by , 13 years ago) |
---|
-
django/contrib/admin/options.py
608 608 description = capfirst(action.replace('_', ' ')) 609 609 return func, action, description 610 610 611 def get_list_display(self, request): 612 """ 613 Return a sequence containing the fields to be displayed on the 614 changelist. 615 """ 616 return self.list_display 617 611 618 def construct_change_message(self, request, form, formsets): 612 619 """ 613 620 Construct a change message from a changed object. … … 1026 1033 actions = self.get_actions(request) 1027 1034 1028 1035 # Remove action checkboxes if there aren't any actions available. 1029 list_display = list(self. list_display)1036 list_display = list(self.get_list_display(request)) 1030 1037 if not actions: 1031 1038 try: 1032 1039 list_display.remove('action_checkbox') -
tests/regressiontests/admin_changelist/tests.py
1 from django.conf import settings 1 2 from django.contrib import admin 2 3 from django.contrib.admin.options import IncorrectLookupParameters 3 4 from django.contrib.admin.views.main import ChangeList 5 from django.contrib.auth.models import User 6 from django.http import QueryDict 4 7 from django.template import Context, Template 5 8 from django.test import TransactionTestCase 6 9 from regressiontests.admin_changelist.models import Child, Parent … … 143 146 self.assertEqual(cl.paginator.count, 30) 144 147 self.assertEqual(cl.paginator.page_range, [1, 2, 3]) 145 148 149 def test_list_display(self): 150 """ 151 Regression tests for #14206: dynamic list_display support. 152 """ 153 parent = Parent.objects.create(name='parent') 154 for i in range(10): 155 Child.objects.create(name='child %s' % i, parent=parent) 146 156 157 user_noparents = User.objects.create( 158 username='noparents', 159 is_superuser=True) 160 user_parents = User.objects.create( 161 username='parents', 162 is_superuser=True) 163 164 # Test with user 'noparents' 165 m = DynamicListDisplayChildAdmin(Child, admin.site) 166 request = ViewMockRequest(user_noparents) 167 response = m.changelist_view(request) 168 self.assertNotContains(response, 'Parent object') 169 170 # Test with user 'parents' 171 m = DynamicListDisplayChildAdmin(Child, admin.site) 172 request = ViewMockRequest(user_parents) 173 response = m.changelist_view(request) 174 self.assertContains(response, 'Parent object') 175 176 # Test default implementation 177 m = ChildAdmin(Child, admin.site) 178 request = ViewMockRequest(user_noparents) 179 response = m.changelist_view(request) 180 self.assertContains(response, 'Parent object') 181 182 147 183 class ChildAdmin(admin.ModelAdmin): 148 184 list_display = ['name', 'parent'] 149 185 list_per_page = 10 … … 157 193 return super(FilteredChildAdmin, self).queryset(request).filter( 158 194 name__contains='filtered') 159 195 196 class DynamicListDisplayChildAdmin(admin.ModelAdmin): 197 list_display = ['name', 'parent'] 198 199 def get_list_display(self, request): 200 list_display = self.list_display 201 if request.user.username == 'noparents': 202 list_display.remove('parent') 203 204 return list_display 205 160 206 class MockRequest(object): 161 207 GET = {} 208 209 210 class ViewMockRequest(object): 211 def __init__(self, user): 212 self.user = user 213 self.COOKIES = {settings.CSRF_COOKIE_NAME: ''} 214 self.GET = QueryDict('') 215 self.POST = QueryDict('') 216 self.META = {} 217 self.method = 'GET' -
docs/ref/contrib/admin/index.txt
807 807 a ``list`` or ``tuple`` of field names that will be displayed as read-only, 808 808 as described above in the :attr:`ModelAdmin.readonly_fields` section. 809 809 810 .. method:: ModelAdmin.get_list_display(self, request) 811 812 .. versionadded:: 1.2.6 813 814 The ``get_list_display`` method is given the ``HttpRequest`` and is 815 expected to return a ``list`` or ``tuple`` of field names that will be 816 displayed on the changelist view as described above in the 817 :attr:`ModelAdmin.list_display` section. 818 810 819 .. method:: ModelAdmin.get_urls(self) 811 820 812 821 .. versionadded:: 1.1