Using a Django custom model method property in order_by()

No, you can’t do that. order_by is applied at the database level, but the database can’t know anything about your custom Python methods.

You can either use the separate fields to order:

Author.objects.order_by('first_name', 'last_name')

or do the ordering in Python:

sorted(Author.objects.all(), key=lambda a: a.full_name)

Leave a Comment