E
E
ekzotika2020-12-22 21:48:55
Django
ekzotika, 2020-12-22 21:48:55

How can I swap the elements of a QuerySet?

I need to swap the elements of the QuerySet so that the elements in it alternate at the output: first, the element where the price is less than a certain one, then it is more than a certain one, after it it is less, more, etc.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Eremin, 2020-12-23
@ekzotika

The most obvious thing is to turn the QuerySet into a list and rearrange it as you like, or collect a new list ...
Or write a raw SQL query using the mechanisms built into your SQL (two queries - goods at a price higher than the specified one, goods less than the specified
price ... add some tricky-key-item-number to each query, combine these queries and sort the result by key)... less than specified). And from these two QuerySets, collect a sheet, taking records from each QuerySet in turn

K
Kirill, 2020-12-22
@kustiktm_kirill

I understand that you need sorting.
https://docs.djangoproject.com/en/3.1/ref/models/q...
https://docs.djangoproject.com/en/3.1/ref/models/q...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question