Django count by group -


i need create report in can value each month of year.

models.py

class ask(models.model):       team = models.charfield(max_length=255)       date = models.datefield() 

in team 3 possible values: a, b, c.

how calculate how many times month added individual team?

i generate report each year.

i suggest add month field model , pass there month on save. run this:

from django.db.models import count ask.objects.filter(date__year='2013').values('month', 'team').annotate(total=count('team')) 

other method use extra parameter , extract month date field:

from django.db.models import count ask.objects.filter(date__year='2013').extra(select={'month': "extract(month date)"}).values('month', 'team').annotate(count('team')) 

but extract method database dependent , example dont`t work in sqlite


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -