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?
Set the
ordering
attribute for the view.If you need to change the ordering dynamically, you can use
get_ordering
instead.If you are always sorting a fixed date field, you may be interested in the
ArchiveIndexView
.Note that
ArchiveIndexView
won't show objects with a date in the future unless you setallow_future
toTrue
.Why don't you override the
get_queryset
method like this: