0

here is my queryset

queryset = (PrathamTeamMembers.objects.
        select_related('state', 'district', 'block', 'personnel_type').
        filter(is_active=1).
        annotate(total_state=Count('state', distinct=True),
                 total_district=Count('district', distinct=True)))

I am expecting result similar to

{
 "total_state": 10
 "total_district": 60
}

But it is not aggregating instead it is grouping by table PrathamTeamMembers primary key. which I want to avoid. Since it is grouping on table primary key so my queryset giving result simlary to ..

[{
  "total_state": 1,
  "total_district": 1
},
{
  "total_state": 1,
  "total_district": 1
},
{
  "total_state": 1,
  "total_district": 1
}
]
Raju Singh
  • 455
  • 1
  • 6
  • 15

1 Answers1

0

use this link for help. GROUB BY ... COUNT/SUM Django ORM equivalent

https://riptutorial.com/django/example/30595/groub-by-----count-sum-django-orm-equivalent

We can perform a GROUP BY ... COUNT or a GROUP BY ... SUM SQL equivalent queries on Django ORM, with the use of annotate(), values(), order_by() and the django.db.models's Count and Sum methods respectfully: