I
I
Igor Lyutoev2017-02-05 23:27:38
Django
Igor Lyutoev, 2017-02-05 23:27:38

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

3 answer(s)
R
Roman Kitaev, 2017-02-05
@deliro

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.

T
Tash1moto, 2017-02-05
@Tash1moto

Entry.objects.order_by('discount_price')

A
Artem Afonin, 2017-02-07
@reli

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 question

Ask a Question

731 491 924 answers to any question