Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Is there Django List View model sort?

I'm using ListView in my Class Based Views and I was wondering if there was a way to display the model object set on the template by sorting it. This is what I have so far:

My views:

class Reviews(ListView):
    model = ProductReview
    paginate_by = 50
    template_name = 'review_system/reviews.html'

The model ProductReview has a date_created field. I'd like to sort the date in descending order. How can I achieve this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Set the ordering attribute for the view.

class Reviews(ListView):
    model = ProductReview
    paginate_by = 50
    template_name = 'review_system/reviews.html'

    ordering = ['-date_created']

If you need to change the ordering dynamically, you can use get_ordering instead.

class Reviews(ListView):
    ...
    def get_ordering(self):
        ordering = self.request.GET.get('ordering', '-date_created')
        # validate ordering here
        return ordering

If you are always sorting a fixed date field, you may be interested in the ArchiveIndexView.

from django.views.generic.dates import ArchiveIndexView

class Reviews(ArchiveIndexView):
    model = ProductReview
    paginate_by = 50
    template_name = 'review_system/reviews.html'
    date_field = "date_created"

Note that ArchiveIndexView won't show objects with a date in the future unless you set allow_future to True.

over 4 years ago · Santiago Trujillo Report

0

Why don't you override the get_queryset method like this:

class Reviews(ListView):
    model = ProductReview
    paginate_by = 50
    template_name = 'review_system/reviews.html'

    def get_queryset(self):
        return YourModel.objects.order_by('model_field_here')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!