Ticket #12875: ticket_12875_for_r16341_with_docs.diff
File ticket_12875_for_r16341_with_docs.diff, 5.9 KB (added by , 13 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
696 696 If this isn't provided, the Django admin will use the model's default 697 697 ordering. 698 698 699 If you need to specify a dynamic order (for example depending on user or 700 language) you can implement a ``get_ordering`` method. This method take 701 ``request`` as parameter and returns the ordering list/tuple. For example:: 702 703 class PersonAdmin(ModelAdmin): 704 705 def get_ordering(self, request): 706 if request.user.is_superuser: 707 return ('rank', ) 708 else: 709 return ('name', ) 710 699 711 .. versionchanged:: 1.4 700 712 701 713 Django honors all elements in the list/tuple; before 1.4, only the first -
django/contrib/admin/options.py
208 208 """ 209 209 qs = self.model._default_manager.get_query_set() 210 210 # TODO: this should be handled by some parameter to the ChangeList. 211 ordering = self. ordering or () # otherwise we might try to *None, which is bad ;)211 ordering = self.get_ordering(request) or self.ordering or () # otherwise we might try to *None, which is bad ;) 212 212 if ordering: 213 213 qs = qs.order_by(*ordering) 214 214 return qs … … 260 260 clean_lookup = LOOKUP_SEP.join(parts) 261 261 return clean_lookup in self.list_filter or clean_lookup == self.date_hierarchy 262 262 263 def get_ordering(self, request): 264 return None 265 266 263 267 class ModelAdmin(BaseModelAdmin): 264 268 "Encapsulates all admin options and functionality for a given model." 265 269 -
django/contrib/admin/views/main.py
27 27 ERROR_FLAG = 'e' 28 28 29 29 IGNORED_PARAMS = ( 30 ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR )30 ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR, ) 31 31 32 32 # Text to display within change-list table cells if the value is blank. 33 33 EMPTY_CHANGELIST_VALUE = ugettext_lazy('(None)') … … 76 76 self.list_editable = () 77 77 else: 78 78 self.list_editable = list_editable 79 self.ordering = self.get_ordering( )79 self.ordering = self.get_ordering(request) 80 80 self.query = request.GET.get(SEARCH_VAR, '') 81 81 self.query_set = self.get_query_set(request) 82 82 self.get_results(request) … … 172 172 ordering = self.lookup_opts.ordering 173 173 return ordering 174 174 175 def get_ordering(self ):175 def get_ordering(self, request): 176 176 params = self.params 177 # For ordering, first check the "ordering" parameter in the admin 177 # For ordering, first check the if exists the "get_ordering" method 178 # in model admin, then check "ordering" parameter in the admin 178 179 # options, then check the object's default ordering. Finally, a 179 180 # manually-specified ordering from the query string overrides anything. 180 ordering = self. _get_default_ordering()181 ordering = self.model_admin.get_ordering(request) or self._get_default_ordering() 181 182 182 183 if ORDER_VAR in params: 183 184 # Clear ordering and used params -
tests/regressiontests/admin_ordering/tests.py
1 1 from django.test import TestCase 2 from django.contrib.auth.models import User 2 3 from django.contrib.admin.options import ModelAdmin 3 4 4 from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering 5 from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering, DynOrderingBandAdmin 5 6 6 7 class TestAdminOrdering(TestCase): 7 8 """ … … 38 39 names = [b.name for b in ma.queryset(None)] 39 40 self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) 40 41 42 def test_dynamic_ordering(self): 43 """ 44 Let's use a custom ModelAdmin that changes the ordering dinamically. 45 """ 46 super_user = User.objects.create(username='admin', is_superuser=True) 47 other_user = User.objects.create(username='other') 48 class DummyRequest(object): pass 49 request = DummyRequest() 50 request.user = super_user 51 ma = DynOrderingBandAdmin(Band, None) 52 names = [b.name for b in ma.queryset(request)] 53 self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) 54 request.user = other_user 55 names = [b.name for b in ma.queryset(request)] 56 self.assertEqual([u'Aerosmith', u'Radiohead', u'Van Halen'], names) 57 58 41 59 class TestInlineModelAdminOrdering(TestCase): 42 60 """ 43 61 Let's make sure that InlineModelAdmin.queryset uses the ordering we define -
tests/regressiontests/admin_ordering/models.py
18 18 class Meta: 19 19 ordering = ('name',) 20 20 21 class DynOrderingBandAdmin(admin.ModelAdmin): 22 23 def get_ordering(self, request): 24 if request.user.is_superuser: 25 return ('rank',) 26 else: 27 return ('name',) 28 21 29 class SongInlineDefaultOrdering(admin.StackedInline): 22 30 model = Song 23 31 24 32 class SongInlineNewOrdering(admin.StackedInline): 25 33 model = Song 26 34 ordering = ('duration', ) 35 36