[Answered ]-Collect ids as list during annotate grouping by different parameters in Django

1👍

Since I have been using postgresql alongside Django .
This worked for me without the distinct part.

There are plenty of articles on Group Concat, haven’t found anything to work.

Here is the solution only for Postgres

Using StringAgg('vehicle_type_id', ',')

from django.contrib.postgres.aggregates import StringAgg

od_engagement = list(FieldOpsBooking.objects.using(
    'analytics').exclude(**exclude_query).filter(**filter_query).values(
    'city_code', 'final_driver_owner_contact').annotate(
    vehicle_types = StringAgg('vehicle_type_id', ','),
    booking_count=Count('final_driver_owner_contact'),
    vehicle_count=Count('final_driver_vehicle_no', distinct=True),
    total_cost=Sum('final_driver_rate'),
    month=TruncMonth('pick_up_at')))

Leave a comment