Ticket #13862: inline_queryset_ordering_2.diff
File inline_queryset_ordering_2.diff, 6.3 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index ffc6e79..1a4ccc4 100644
a b class BaseModelAdmin(object): 67 67 prepopulated_fields = {} 68 68 formfield_overrides = {} 69 69 readonly_fields = () 70 ordering = None 70 71 71 72 def __init__(self): 72 73 overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy() … … class BaseModelAdmin(object): 189 190 def get_readonly_fields(self, request, obj=None): 190 191 return self.readonly_fields 191 192 193 def queryset(self, request): 194 """ 195 Returns a QuerySet of all model instances that can be edited by the 196 admin site. This is used by changelist_view. 197 """ 198 qs = self.model._default_manager.get_query_set() 199 # TODO: this should be handled by some parameter to the ChangeList. 200 ordering = self.ordering or () # otherwise we might try to *None, which is bad ;) 201 if ordering: 202 qs = qs.order_by(*ordering) 203 return qs 204 192 205 class ModelAdmin(BaseModelAdmin): 193 206 "Encapsulates all admin options and functionality for a given model." 194 207 … … class ModelAdmin(BaseModelAdmin): 202 215 date_hierarchy = None 203 216 save_as = False 204 217 save_on_top = False 205 ordering = None206 218 inlines = [] 207 219 208 220 # Custom templates (designed to be over-ridden in subclasses) … … class ModelAdmin(BaseModelAdmin): 325 337 'delete': self.has_delete_permission(request), 326 338 } 327 339 328 def queryset(self, request):329 """330 Returns a QuerySet of all model instances that can be edited by the331 admin site. This is used by changelist_view.332 """333 qs = self.model._default_manager.get_query_set()334 # TODO: this should be handled by some parameter to the ChangeList.335 ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)336 if ordering:337 qs = qs.order_by(*ordering)338 return qs339 340 340 def get_fieldsets(self, request, obj=None): 341 341 "Hook for specifying fieldsets for the add form." 342 342 if self.declared_fieldsets: … … class InlineModelAdmin(BaseModelAdmin): 1279 1279 fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj)) 1280 1280 return [(None, {'fields': fields})] 1281 1281 1282 def queryset(self, request):1283 return self.model._default_manager.all()1284 1285 1282 class StackedInline(InlineModelAdmin): 1286 1283 template = 'admin/edit_inline/stacked.html' 1287 1284 -
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index ac517e8..8e64ec7 100644
a b all the same functionality as well as some of its own: 1093 1093 doesn't directly correlate to the number of objects, but can if the value 1094 1094 is small enough. See :ref:`model-formsets-max-num` for more information. 1095 1095 1096 .. attribute:: InlineModelAdmin.ordering 1097 1098 Set ``ordering`` to specify how inlines on the admin change form page should 1099 be ordered. This should be a list or tuple in the same format as a model's 1100 ``ordering`` parameter. 1101 1102 If this isn't provided, the Django admin will use the model's default ordering. 1103 1104 .. admonition:: Note 1105 1106 Django will only honor the first element in the list/tuple; any others 1107 will be ignored. 1108 1096 1109 .. attribute:: InlineModelAdmin.raw_id_fields 1097 1110 1098 1111 By default, Django's admin uses a select-box interface (<select>) for -
tests/regressiontests/admin_ordering/models.py
diff --git a/tests/regressiontests/admin_ordering/models.py b/tests/regressiontests/admin_ordering/models.py index ad63685..fb76677 100644
a b 1 1 # coding: utf-8 2 2 from django.db import models 3 from django.contrib import admin 3 4 4 5 class Band(models.Model): 5 6 name = models.CharField(max_length=100) … … class Band(models.Model): 8 9 9 10 class Meta: 10 11 ordering = ('name',) 12 13 class Song(models.Model): 14 band = models.ForeignKey(Band) 15 name = models.CharField(max_length=100) 16 duration = models.IntegerField() 17 18 class Meta: 19 ordering = ('name',) 20 21 class SongInlineDefaultOrdering(admin.StackedInline): 22 model = Song 23 24 class SongInlineNewOrdering(admin.StackedInline): 25 model = Song 26 ordering = ('duration', ) -
tests/regressiontests/admin_ordering/tests.py
diff --git a/tests/regressiontests/admin_ordering/tests.py b/tests/regressiontests/admin_ordering/tests.py index f63f202..1b91ac0 100644
a b 1 1 from django.test import TestCase 2 2 from django.contrib.admin.options import ModelAdmin 3 3 4 from models import Band 4 from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering 5 5 6 6 class TestAdminOrdering(TestCase): 7 7 """ … … class TestAdminOrdering(TestCase): 37 37 ma = BandAdmin(Band, None) 38 38 names = [b.name for b in ma.queryset(None)] 39 39 self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) 40 41 class TestInlineModelAdminOrdering(TestCase): 42 """ 43 Let's make sure that InlineModelAdmin.queryset uses the ordering we define 44 in InlineModelAdmin. 45 """ 46 47 def setUp(self): 48 b = Band(name='Aerosmith', bio='', rank=3) 49 b.save() 50 self.b = b 51 s1 = Song(band=b, name='Pink', duration=235) 52 s1.save() 53 s2 = Song(band=b, name='Dude (Looks Like a Lady)', duration=264) 54 s2.save() 55 s3 = Song(band=b, name='Jaded', duration=214) 56 s3.save() 57 58 def test_default_ordering(self): 59 """ 60 The default ordering should be by name, as specified in the inner Meta 61 class. 62 """ 63 inline = SongInlineDefaultOrdering(self.b, None) 64 names = [s.name for s in inline.queryset(None)] 65 self.assertEqual([u'Dude (Looks Like a Lady)', u'Jaded', u'Pink'], names) 66 67 def test_specified_ordering(self): 68 """ 69 Let's check with ordering set to something different than the default. 70 """ 71 inline = SongInlineNewOrdering(self.b, None) 72 names = [s.name for s in inline.queryset(None)] 73 self.assertEqual([u'Jaded', u'Pink', u'Dude (Looks Like a Lady)'], names)