Opened 17 years ago

Closed 13 years ago

#7309 closed Bug (fixed)

NFA: Don't override order_by if no default ordering is specified

Reported by: lukas@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: order_by nfa-someday
Cc: simon@…, zerok@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

newforms-admin has this incredibly useful feature where it allowas you to override the modelAdmin's queryset method like this:

    def queryset(self, request):
        return Chain.objects.all().order_by('treeid', 'level')

And there, you already see the problem: Newforms-admin always changes the order_by clause, even if nothing else was specified in model.Meta or in it's modelAdmin class, or via a GET parameter. This makes it impossible to have the queryset from the aformentioned method presorted.

The reason I need it is quite simple, I need to sort with two or more fields, and nfa only support sorting by one single field :/

So far, I have no idea how to overcome this.

Change History (11)

comment:1 by Karen Tracey <kmtracey@…>, 16 years ago

Keywords: nfa-someday added

comment:2 by anonymous, 16 years ago

Cc: simon@… added

comment:3 by Horst Gutmann <zerok@…>, 16 years ago

Cc: zerok@… added

Is this even valid anymore? From what I can see in the ModelAdmin class, ordering is now handled using the ordering class-element, which can be any list/tuple (I think this happened somewhere around #7484):

{{

def queryset(self, request):

"""
Returns a QuerySet of all model instances that can be edited by the
admin site. This is used by changelist_view.
"""
qs = self.model._default_manager.get_query_set()
# TODO: this should be handled by some parameter to the ChangeList.
ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
if ordering:

qs = qs.order_by(*ordering)

return qs

}}

comment:4 by lukas@…, 16 years ago

I just tried it again with newforms-admin as of commit 7875, but it still doesn't do the ordering properly in the admin interface.

comment:5 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:6 by James Bennett, 16 years ago

milestone: 1.0post-1.0
Version: newforms-adminSVN

Since you can override queryset() to get initial multi-field ordering, I'm punting this to at least post-1.0.

comment:7 by Erik Stein, 16 years ago

The problem seems to be that in ChangeList.get_query_set an ordering is added to the queryset, ignoring an already applied one. This just clears the previous ordering.

class QuerySet(object):
    ...
    def order_by(self, *field_names):
        """
        Returns a new QuerySet instance with the ordering changed.
        """
        assert self.query.can_filter(), \
                "Cannot reorder a query once a slice has been taken."
        obj = self._clone()
        obj.query.clear_ordering()
        obj.query.add_ordering(*field_names)
        return obj

comment:8 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:9 by Julien Phalip, 14 years ago

#10212 is a dupe and has a patch.

comment:10 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:11 by Luke Plant, 13 years ago

Resolution: fixed
Status: newclosed

In [16316]:

Fixed #11868 - Multiple sort in admin changelist.

Many thanks to bendavis78 for the initial patch, and for input from others.

Also fixed #7309. If people were relying on the undocumented default ordering
applied by the admin before, they will need to add 'ordering = -pk' to
their ModelAdmin.

Note: See TracTickets for help on using tickets.
Back to Top