Ticket #13862: inline_queryset_ordering_2_and_docs_correction.diff
File inline_queryset_ordering_2_and_docs_correction.diff, 9.2 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 712f07e..7a42e94 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): 1257 1257 fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj)) 1258 1258 return [(None, {'fields': fields})] 1259 1259 1260 def queryset(self, request):1261 return self.model._default_manager.all()1262 1263 1260 class StackedInline(InlineModelAdmin): 1264 1261 template = 'admin/edit_inline/stacked.html' 1265 1262 -
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 15a5ba2..6f26a7b 100644
a b The ``field_options`` dictionary can have the following keys: 172 172 .. versionadded:: 1.2 173 173 174 174 ``fields`` can contain values defined in 175 :attr:` ModelAdmin.readonly_fields` to be displayed as read-only.175 :attr:`~ModelAdmin.readonly_fields` to be displayed as read-only. 176 176 177 177 * ``classes`` 178 178 A list containing extra CSS classes to apply to the fieldset. … … See ``InlineModelAdmin`` objects below. 493 493 494 494 .. attribute:: ModelAdmin.ordering 495 495 496 Set ``ordering`` to specify how objects on the admin change list page should be497 ordered. This should be a list or tuple in the same format as a model's 498 ``ordering`` parameter.496 Set ``ordering`` to specify how lists of objects should be ordered in the 497 Django admin views. This should be a list or tuple in the same format as a 498 model's ``ordering`` parameter. 499 499 500 500 If this isn't provided, the Django admin will use the model's default ordering. 501 501 … … The difference between these two is merely the template used to render them. 1060 1060 ``InlineModelAdmin`` options 1061 1061 ----------------------------- 1062 1062 1063 The ``InlineModelAdmin`` class is a subclass of ``ModelAdmin`` so it inherits 1064 all the same functionality as well as some of its own: 1063 ``InlineModelAdmin`` shares many of the same features as ``ModelAdmin``, and 1064 adds some of its own (the shared features are actually defined in the 1065 ``BaseModelAdmin`` superclass). The shared features are: 1066 1067 - :attr:`~ModelAdmin.form` 1068 - :attr:`~ModelAdmin.fieldsets` 1069 - :attr:`~ModelAdmin.fields` 1070 - :attr:`~ModelAdmin.exclude` 1071 - :attr:`~ModelAdmin.filter_horizontal` 1072 - :attr:`~ModelAdmin.filter_vertical` 1073 - :attr:`~ModelAdmin.prepopulated_fields` 1074 - :attr:`~ModelAdmin.radio_fields` 1075 - :attr:`~ModelAdmin.raw_id_fields` 1076 1077 .. versionadded:: 1.1 1078 1079 - :meth:`~ModelAdmin.formfield_for_foreignkey` 1080 - :meth:`~ModelAdmin.formfield_for_manytomany` 1081 1082 .. versionadded:: 1.2 1083 1084 - :attr:`~ModelAdmin.readonly_fields` 1085 - :attr:`~ModelAdmin.formfield_overrides` 1086 1087 .. versionadded:: 1.3 1088 1089 - :attr:`~ModelAdmin.ordering` 1090 - :meth:`~ModelAdmin.queryset` 1091 1092 The ``InlineModelAdmin`` class adds: 1065 1093 1066 1094 .. attribute:: InlineModelAdmin.model 1067 1095 … … all the same functionality as well as some of its own: 1079 1107 many possibilities of customization. Inlines are built around 1080 1108 :ref:`model formsets <model-formsets>`. 1081 1109 1082 .. attribute:: InlineModelAdmin.form1083 1084 The value for ``form`` defaults to ``ModelForm``. This is what is passed1085 through to ``inlineformset_factory`` when creating the formset for this1086 inline.1087 1088 1110 .. _ref-contrib-admin-inline-extra: 1089 1111 1090 1112 .. attribute:: InlineModelAdmin.extra 1091 1113 1092 1093 1114 This controls the number of extra forms the formset will display in addition 1094 1115 to the initial forms. See the 1095 1116 :doc:`formsets documentation </topics/forms/formsets>` for more information. … … all the same functionality as well as some of its own: 1111 1132 doesn't directly correlate to the number of objects, but can if the value 1112 1133 is small enough. See :ref:`model-formsets-max-num` for more information. 1113 1134 1114 .. attribute:: InlineModelAdmin.raw_id_fields1115 1116 By default, Django's admin uses a select-box interface (<select>) for1117 fields that are ``ForeignKey``. Sometimes you don't want to incur the1118 overhead of having to select all the related instances to display in the1119 drop-down.1120 1121 ``raw_id_fields`` is a list of fields you would like to change into a1122 ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``::1123 1124 class BookInline(admin.TabularInline):1125 model = Book1126 raw_id_fields = ("pages",)1127 1128 1129 1135 .. attribute:: InlineModelAdmin.template 1130 1136 1131 1137 The template used to render the inline on the page. -
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..8f5a6bd 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) 74 No newline at end of file