Opened 4 years ago

Closed 16 months ago

#18355 closed New feature (fixed)

Add ordering mixin for class based generic views

Reported by: aaugustin Owned by: pjrharley
Component: Generic views Version: master
Severity: Normal Keywords:
Cc: marc.tamlyn@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Currently, if the model used by by a date-based generic view doesn't have an ordering and pagination is enabled, results will be random. Defining an ordering on the model isn't totally satisfying because it has global effects, and it adds an overhead to every query involving the model.

Another workaround is to define queryset = MyModel.objects.order_by('-<date_field>') in every view. But CBVs are precisely about removing as much boilerplate as possible.

So here's a draft of an API for ordering:

class BaseDateListView(...):

    ordering = None

    def get_ordering(self):
        return self.get_date_field() if self.ordering is None else self.ordering
    def get_dated_queryset(self, **lookups):


    if not qs.ordered():
        qs = qs.order_by(self.get_ordering())


self.get_ordering() could return '-<date_field>' for the archive view for backwards compatibility.

This would be a better fix for #18354.

Change History (12)

comment:1 Changed 4 years ago by aaugustin

  • Owner changed from nobody to aaugustin

comment:2 Changed 4 years ago by jezdez

  • Triage Stage changed from Unreviewed to Accepted

I accept the problem in general, but don't see how this is limited to date based views. This is perfect for a mixin, so let's write a OrderingViewMixin or similar.

comment:3 Changed 4 years ago by jezdez

  • Summary changed from Date-based generic views should enforce ordering to Add ordering mixin for class based generic views

comment:4 Changed 3 years ago by mjtamlyn

  • Cc marc.tamlyn@… added

Would this functionality apply only to list views? This would certainly give a good advantage as one's instinct is to apply the get_queryset changes to every view with the same Model, but ordering is rather useless when we're simply doing a DetailView (and could impose a performance problem). So the code would perhaps live in BaseListView rather than MultipleObjectMixin.

That said, if this functionality is only hitting list views, it actually fits in with a larger subject area of search/ordering/filtering as defined by the user. In reality this is probably just one of the first hooks we might need to implement that kind of functionality well, but it may be worth considering the larger list-view-customisation issue.

comment:5 Changed 3 years ago by aaugustin

The goals were clear to me when I was working on the date-based views, but unfortunately I've lost track of it since then...

I think it's just about implementing the API in the ticket description, documenting it, and taking advantage of it in the date-based views while preserving backwards-compat.

You're very welcome to take over on this ticket if you'd like to as it's pretty low on my priority list.

comment:6 Changed 2 years ago by aaugustin

  • Status changed from new to assigned

comment:7 Changed 2 years ago by aaugustin

  • Owner aaugustin deleted
  • Status changed from assigned to new

comment:8 Changed 2 years ago by anonymous

  • Has patch set
  • Owner set to anonymous
  • Status changed from new to assigned

I've added a PR for this here:

Not sure if the approach is right - feedback welcome. I suspect the docs need improvement - more than happy to do that if the code is looking good.

comment:9 Changed 2 years ago by pjrharley

  • Owner changed from anonymous to pjrharley

Try assigning to myself while signed in instead...

comment:10 Changed 18 months ago by timo

  • Patch needs improvement set

I left comments for improvement on PR. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:11 Changed 16 months ago by pjrharley

  • Patch needs improvement unset

Patch updated. Thanks for the review.

comment:12 Changed 16 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 2724cdbff635a40819f206411de23e9b14867a58:

Fixed #18355 -- Added ordering options to list based generic views.

Added MultipleObjectMixin.ordering and get_ordering().

Refs #21450.

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