I query a model,
Members.objects.all()
and it returns say
Eric, Salesman, X-Shop
Freddie, Manager, X2-Shop
Teddy, Salesman, X2-Shop
Sean, Manager, X2-Shop
What i want is, to know the best Django way to fire a group_by query to my db, as like,
Members.objects.all().group_by('designation')
Which doesn't work of course. I know we can do some tricks on "django/db/models/query.py", but i am just curious to know how to do it without patching.
If I'm not mistaking you can use, whatever-query-set.group_by=['field']
The document says that you can use values to group the queryset .
You can find all the books and group them by name using this code:
You can watch some cheet sheet here.
If you mean to do aggregation you can use the aggregation features of the ORM:
This results in a query similar to
and the output would be of the form