Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question