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
Post a Comment