5👍
Solution for PostgreSQL grouping by name field in combination with ArrayAgg
and the distinct=True
flag:
from django.contrib.postgres.aggregates.general import ArrayAgg
from django.db.models import Count
Model.objects.values('name').annotate(
count_total=Count('count'),
site_list=ArrayAgg('site', distinct=True),
)
1👍
If you are using postgres you can use ArrayAgg
docs – https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/aggregates/
You can use values to group by and something like the following should do the trick. Model.objects.values('name').distinct().annotate(site_list=ArrayAgg('site'), count_total=Count('count'))
- [Django]-How start server Django in the VM (Vagrant)
- [Django]-IF statement not working with list difference
- [Django]-Python catch previous exception in broad handler
- [Django]-Django Apache wsgi changes python version
Source:stackexchange.com