[Fixed]-Grouping QuerySet in Django

0👍

I’ve found the solution: module django-group-by allows to apply grouping to querysets, like this:

Model3.objects.group_by('model2__model1')

(You need to mix GroupByMixin into the model manager)

1👍

I know you can do the following

Model3.objects.order_by('model2__model1_id', 'model2__date').distinct('model2__model1_id')

Unfortunately it’s not sorted by model2.date, but for each model1.id the model2.date is the least, and it’s sorted by model1 id

Leave a comment