H
H
Herr_Konrad2021-11-09 08:34:09
Django
Herr_Konrad, 2021-11-09 08:34:09

How to efficiently sort a QuerySet?

Hello! There is a QuerySet that stores the number of likes for each group in the social network. The number of likes is initialized as a @property for the Django model. My task is to sort this QuerySet in descending order (most likes come first). I am using code like this:

sorted_groups = sorted(queryset, key=lambda g: (-g.posts_likes))

Everything works correctly, but very slowly. Can you please tell me how to optimize this sorting? The Django documentation states that iterator() can be sped up, is it worth digging in this direction?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-09
@Herr_Konrad

Digging is in the direction of sorting by the DBMS. Your question has already been asked here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question