Django orm query_set

Solution 1:

If you are using Postgres, you can try this:

from django.contrib.postgres.aggregates import ArrayAgg

a = TestManager.objects.filter(company_code__in=sub_company).values(
    'vehicle__name',
    'vehicle__number'
).annotate(
    vc=Count("vehicle__name"),
    user_id_first_names=ArrayAgg('user_id_first_name', distinct=True)
).order_by('vehicle__name')