Home > Software design >  how Count field using django orm
how Count field using django orm

Time:04-10

Hi Everyone i trying to count active and inactive status sepratally, below mention code to count all, i need to count active and inactive status sepratally,pls help me out. pls refere sample data

Type.objects.all().aggregate(Count('status'))

output=5 

table formet

CodePudding user response:

Look at https://pythonguides.com/python-django-group-by/

Type.objects.values('status').annotate(
    status_count=Count('id')
).values(
    'status', 'status_count'
).order_by()

EDIT: count only in active status

Type.objects.filter(status='active').aggregate(
    active_count=Count('status')
).values('active_count')
  • Related