Ticket #13862: inline_queryset_ordering_2_and_docs_correction.diff

File inline_queryset_ordering_2_and_docs_correction.diff, 9.2 KB (added by Greg Turner, 14 years ago)

Added docs corrections from #14816.

  • 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):  
    6767    prepopulated_fields = {}
    6868    formfield_overrides = {}
    6969    readonly_fields = ()
     70    ordering = None
    7071
    7172    def __init__(self):
    7273        overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy()
    class BaseModelAdmin(object):  
    189190    def get_readonly_fields(self, request, obj=None):
    190191        return self.readonly_fields
    191192
     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
    192205class ModelAdmin(BaseModelAdmin):
    193206    "Encapsulates all admin options and functionality for a given model."
    194207
    class ModelAdmin(BaseModelAdmin):  
    202215    date_hierarchy = None
    203216    save_as = False
    204217    save_on_top = False
    205     ordering = None
    206218    inlines = []
    207219
    208220    # Custom templates (designed to be over-ridden in subclasses)
    class ModelAdmin(BaseModelAdmin):  
    325337            'delete': self.has_delete_permission(request),
    326338        }
    327339
    328     def queryset(self, request):
    329         """
    330         Returns a QuerySet of all model instances that can be edited by the
    331         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 qs
    339 
    340340    def get_fieldsets(self, request, obj=None):
    341341        "Hook for specifying fieldsets for the add form."
    342342        if self.declared_fieldsets:
    class InlineModelAdmin(BaseModelAdmin):  
    12571257        fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
    12581258        return [(None, {'fields': fields})]
    12591259
    1260     def queryset(self, request):
    1261         return self.model._default_manager.all()
    1262 
    12631260class StackedInline(InlineModelAdmin):
    12641261    template = 'admin/edit_inline/stacked.html'
    12651262
  • 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:  
    172172        .. versionadded:: 1.2
    173173
    174174        ``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.
    176176
    177177    * ``classes``
    178178        A list containing extra CSS classes to apply to the fieldset.
    See ``InlineModelAdmin`` objects below.  
    493493
    494494.. attribute:: ModelAdmin.ordering
    495495
    496 Set ``ordering`` to specify how objects on the admin change list page should be
    497 ordered. This should be a list or tuple in the same format as a model's
    498 ``ordering`` parameter.
     496Set ``ordering`` to specify how lists of objects should be ordered in the
     497Django admin views. This should be a list or tuple in the same format as a
     498model's ``ordering`` parameter.
    499499
    500500If this isn't provided, the Django admin will use the model's default ordering.
    501501
    The difference between these two is merely the template used to render them.  
    10601060``InlineModelAdmin`` options
    10611061-----------------------------
    10621062
    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
     1064adds 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
     1092The ``InlineModelAdmin`` class adds:
    10651093
    10661094.. attribute:: InlineModelAdmin.model
    10671095
    all the same functionality as well as some of its own:  
    10791107    many possibilities of customization. Inlines are built around
    10801108    :ref:`model formsets <model-formsets>`.
    10811109
    1082 .. attribute:: InlineModelAdmin.form
    1083 
    1084     The value for ``form`` defaults to ``ModelForm``. This is what is passed
    1085     through to ``inlineformset_factory`` when creating the formset for this
    1086     inline.
    1087 
    10881110.. _ref-contrib-admin-inline-extra:
    10891111
    10901112.. attribute:: InlineModelAdmin.extra
    10911113
    1092 
    10931114    This controls the number of extra forms the formset will display in addition
    10941115    to the initial forms. See the
    10951116    :doc:`formsets documentation </topics/forms/formsets>` for more information.
    all the same functionality as well as some of its own:  
    11111132    doesn't directly correlate to the number of objects, but can if the value
    11121133    is small enough. See :ref:`model-formsets-max-num` for more information.
    11131134
    1114 .. attribute:: InlineModelAdmin.raw_id_fields
    1115 
    1116     By default, Django's admin uses a select-box interface (<select>) for
    1117     fields that are ``ForeignKey``. Sometimes you don't want to incur the
    1118     overhead of having to select all the related instances to display in the
    1119     drop-down.
    1120 
    1121     ``raw_id_fields`` is a list of fields you would like to change into a
    1122     ``Input`` widget for either a ``ForeignKey`` or ``ManyToManyField``::
    1123 
    1124         class BookInline(admin.TabularInline):
    1125             model = Book
    1126             raw_id_fields = ("pages",)
    1127 
    1128 
    11291135.. attribute:: InlineModelAdmin.template
    11301136
    11311137    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  
    11# coding: utf-8
    22from django.db import models
     3from django.contrib import admin
    34
    45class Band(models.Model):
    56    name = models.CharField(max_length=100)
    class Band(models.Model):  
    89
    910    class Meta:
    1011        ordering = ('name',)
     12
     13class 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
     21class SongInlineDefaultOrdering(admin.StackedInline):
     22    model = Song
     23
     24class 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  
    11from django.test import TestCase
    22from django.contrib.admin.options import ModelAdmin
    33
    4 from models import Band
     4from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering
    55
    66class TestAdminOrdering(TestCase):
    77    """
    class TestAdminOrdering(TestCase):  
    3737        ma = BandAdmin(Band, None)
    3838        names = [b.name for b in ma.queryset(None)]
    3939        self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names)
     40
     41class 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
Back to Top