Ticket #10212: admin_ordering.diff

File admin_ordering.diff, 2.0 KB (added by mzzzzc, 16 years ago)

More general patch. Also allows multi-level ordering and ordering given in query_set.extra().

  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index b450e89..08d1919 100644
    a b class ChangeList(object):  
    131131
    132132    def get_ordering(self):
    133133        lookup_opts, params = self.lookup_opts, self.params
    134         # For ordering, first check the "ordering" parameter in the admin
    135         # options, then check the object's default ordering. If neither of
    136         # those exist, order descending by ID by default. Finally, look for
    137         # manually-specified ordering from the query string.
    138         ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
    139 
    140         if ordering[0].startswith('-'):
    141             order_field, order_type = ordering[0][1:], 'desc'
     134        # If the root query set has no ordering, order descending by ID.
     135        # Set explicit ordering if it is specified in the query string.
     136        if self.root_query_set.query.has_ordering():
     137            # self.root_queryset already has ordering, don't over-ride it
     138            order_field, order_type = None, 'asc'
    142139        else:
    143             order_field, order_type = ordering[0], 'asc'
     140            order_field, order_type = lookup_opts.pk.name, 'desc'
     141
    144142        if ORDER_VAR in params:
    145143            try:
    146144                field_name = self.list_display[int(params[ORDER_VAR])]
  • django/db/models/sql/query.py

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    index 3381b60..de0e35e 100644
    a b class BaseQuery(object):  
    596596                result.append(elt)
    597597        return result, params
    598598
     599    def has_ordering(self):
     600        """
     601        Return True if ordering will be set on this query.
     602        """
     603        return bool(self.extra_order_by or self.order_by or (self.default_ordering and self.model._meta.ordering))
     604
    599605    def get_ordering(self):
    600606        """
    601607        Returns list representing the SQL elements in the "order by" clause.
Back to Top