Ticket #12875: ticket_12875_for_r12454_with_tests.diff
File ticket_12875_for_r12454_with_tests.diff, 4.3 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/options.py
261 261 ) 262 262 return urlpatterns 263 263 264 def get_ordering(self, request): 265 return None 266 264 267 def urls(self): 265 268 return self.get_urls() 266 269 urls = property(urls) … … 325 328 """ 326 329 qs = self.model._default_manager.get_query_set() 327 330 # TODO: this should be handled by some parameter to the ChangeList. 328 ordering = self. ordering or () # otherwise we might try to *None, which is bad ;)331 ordering = self.get_ordering(request) or self.ordering or () # otherwise we might try to *None, which is bad ;) 329 332 if ordering: 330 333 qs = qs.order_by(*ordering) 331 334 return qs -
django/contrib/admin/views/main.py
63 63 if ERROR_FLAG in self.params: 64 64 del self.params[ERROR_FLAG] 65 65 66 self.order_field, self.order_type = self.get_ordering( )66 self.order_field, self.order_type = self.get_ordering(request) 67 67 self.query = request.GET.get(SEARCH_VAR, '') 68 68 self.query_set = self.get_query_set() 69 69 self.get_results(request) … … 130 130 self.multi_page = multi_page 131 131 self.paginator = paginator 132 132 133 def get_ordering(self ):133 def get_ordering(self, request): 134 134 lookup_opts, params = self.lookup_opts, self.params 135 # For ordering, first check the "ordering" parameter in the admin 135 # For ordering, first check if exists the "get_ordering" method 136 # in model admin, then check the "ordering" parameter in admin 136 137 # options, then check the object's default ordering. If neither of 137 138 # those exist, order descending by ID by default. Finally, look for 138 139 # manually-specified ordering from the query string. 139 ordering = self.model_admin. ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]140 ordering = self.model_admin.get_ordering(request) or self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name] 140 141 141 142 if ordering[0].startswith('-'): 142 143 order_field, order_type = ordering[0][1:], 'desc' -
tests/regressiontests/admin_ordering/models.py
42 42 >>> [b.name for b in ma.queryset(None)] 43 43 [u'Radiohead', u'Van Halen', u'Aerosmith'] 44 44 45 46 Let's use a custom ModelAdmin that changes the ordering dinamically. 47 48 >>> class DynOrderingBandAdmin(ModelAdmin): 49 ... def get_ordering(self, request): 50 ... if request.user.is_superuser: 51 ... return ('rank',) 52 ... else: 53 ... return ('name',) 54 55 >>> from django.contrib.auth.models import User 56 >>> super_user = User.objects.create(username='admin', is_superuser=True) 57 >>> other_user = User.objects.create(username='other') 58 >>> class DummyRequest(object): pass 59 >>> request = DummyRequest() 60 61 Ordering will be different for a super user and a normal one 62 63 >>> request.user = super_user 64 >>> ma = DynOrderingBandAdmin(Band, None) 65 >>> [b.name for b in ma.queryset(request)] 66 [u'Radiohead', u'Van Halen', u'Aerosmith'] 67 >>> request.user = other_user 68 >>> [b.name for b in ma.queryset(request)] 69 [u'Aerosmith', u'Radiohead', u'Van Halen'] 70 45 71 """ 46 72 } -
tests/regressiontests/modeladmin/models.py
8 8 name = models.CharField(max_length=100) 9 9 bio = models.TextField() 10 10 sign_date = models.DateField() 11 11 12 12 def __unicode__(self): 13 13 return self.name 14 14 … … 147 147 >>> from django import forms 148 148 >>> class AdminBandForm(forms.ModelForm): 149 149 ... delete = forms.BooleanField() 150 ... 150 ... 151 151 ... class Meta: 152 152 ... model = Band 153 153