Answer the question
In order to leave comments, you need to log in
Django. How to sort queryset by custom method?
There is a product model, with a custom discount_price method that calculates the discounted price of the product.
The calculation is not trivial and is not done in one request.
I can not find a good way to sort and make selections at this price.
Who faced a similar problem, tell me where to dig.
Answer the question
In order to leave comments, you need to log in
There is no normal way to sort "by method", leaving the selection as a QuerySet. You can transfer the logic to SQL and sort there.
If, for example, you need to sort by another field of another table, you make an annotation, and you can filter or sort by that lookup. Saved me a lot at the time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question