About Sorting a List of Model Instances with Attrgetter
Use operator.attrgetter
to sort a list of model instances by one or more fields, for example:
1 2 3 4 5 6 7 |
|
Note that this option doesn't work if your fields are nullable. And whenever possible, you should instead do the sorting at the database level, that is, QuerySets.
Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2 Python 3
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.