I have the following model:
class Entry(models.Model):
name = models.Charfield(max_length=255)
client = models.Charfield(max_length=255)
client
is the name of the client as could have values like facebook
, google
, and so on.
Is it possible to order the queryset so that the result is alternating the values of client
?
What I expect is something like this:
Entry.objects.order_by('alternate client') -->
| client | name |
| google | robert |
| facebook | linda |
| google | kate |
| facebook | jack |
| google | nina |
| facebook | pierre |
I am using django2.x and postgres if that helps.
EDIT:
Some additional info / requirements.
- I have about 10 to 20 differtent clients
- Entry does also have a
created
DateField. If possible result should also be ordered by date - I want to use pagination for Entry so solution should be using django's ORM
Thats not the most performant way, but works:
Expected output:
This is python3 code, but if you want to use it with python 2, change the
zip_longest
function toizip_longest
.This code is nice, because we still work with Queryset, so all other Sorting, Ordering, Managers, Pagination and other stuff will still work.
Since you use Postgres you can use its Window Functions which perform a calculation across a set of table rows that are somehow related to the current row. Another good information relies in the fact that you use Django2.x which supports Window Functions(Django docs) which allows adding an
OVER
clause toQuerysets
.Your use-case can be resolved with Single ORM query like:
Output:
The raw SQL looks like:
Window functions are declared just as an aggregate function followed by an OVER clause, which indicates exactly how rows are being grouped. The group of rows onto which the window function is applied is called "partition".
You can notice that we grouped the rows by 'client' field and you can conclude that in our example we will have the two partitions. First partition will contain all the 'facebook' entries and second partition will contain all the 'google' entries. In its basic form, a partition is no different than a normal aggregate function group: simply a set of rows considered "equal" by some criteria, and the function will be applied over all these rows to return a single result.
In your example we can use the
row_number
window function which simply returns the index of the current row within its partition starting from 1. That helped me to establish the alternating output inorder_by('row_number', 'client')
.Additional information:
If you want to achieve an order like this:
'facebook','facebook', 'google','google','facebook','facebook','google','google'
or
'facebook','facebook','facebook','google','google','google','facebook', 'facebook','facebook'
You will need to do one small math related modification of the previous query like:
Output:
You can notice that
GROUP_SIZE
constant defines how many items will be in the each alternating group.P.S.
Thank you for asking this question because it helped me to better understand the Window Functions.
Happy coding :)
Maybe the following will work, with numpy module