1

Details.objects.all()

Dept      Gender   Shift
Software  Male     Day
Software2 Female   Night

what i want is json like {"Gender":"Male", "count":2} using queryset. count is the number of male and female in specific dept.

im new to djnago and have tried this

Details.objects.values("Gender").annotate(count=Count("Gender")).order_by("Shift") 

where i get

[{'count': 3, 'Gender': u'Female'}, {'count': 1, 'Gender': u'Male'}, {'count': 3, 'Gender': u'Female'}, {'count': 3, 'Gender': u'Male'}] 

of both departments...I want one dept at a time(Software/Software2). Please help:) Thanks.

6
  • share your expected output format Commented Jul 18, 2017 at 6:39
  • {"Gender":"Male", "count":2} this is my expected output. i need to get theie count and gender by dept Commented Jul 18, 2017 at 6:52
  • Have you tried Details.objects.values("Dept", "Gender", "Shift").annotate(count=Count("Gender")) Commented Jul 18, 2017 at 7:24
  • @FazilZaid thank you:) but can u help me make it more precise so i get {"Gender":"Male", "count":2} from software dept only. Commented Jul 18, 2017 at 7:35
  • Details.objects.filter(dept='Software').values("Dept", "Gender", "Shift").annotate(count=Count("Gender")) Commented Jul 18, 2017 at 7:38

2 Answers 2

1

The following query takes gender and department as input and gives out count as output in JSON.

day_male_count = Details.objects.filter(gender='Male', department='Software', shift='Day').count()

night_male_count = Details.objects.filter(gender='Male', department='Software', shift='Night').count()

I feel like this may not be what you were asking, is it?

Sign up to request clarification or add additional context in comments.

2 Comments

this gives a count of male in software dept. i want male count who works in software dept at day shift and night shift separetely.
@meow Changed the answer. Please check now.
0

The Django ORM makes this quite difficult to do using annotations and aggregates so I opted for a more pure-python approach.

from collections import Counter
d = list(Details.objects.values_list('Dept', 'Gender', 'Shift'))
response = [i + (c[i],) for i in Counter(d)]

What we are doing is pulling out the rows as tuples and then passing them into a python Counter which returns a dict-like object where the keys are the original tuples and the values are a count of the number of tuple occurrences.

This gives:

[
    ('software1', 'male', 'night', 2), 
    ('software1', 'female', 'day', 1), 
    ('software2', 'male', 'day', 1), 
    ('software2', 'female', 'day', 1), 
    ('software1', 'female', 'night', 1)
]

To dictify the result we run:

final = [{'Dept': i[0], 'Gender': i[1], 'Shift': i[2], 'Count': i[3]} for i in response]

Hope I helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.