Reputation: 323
I need to create a report in which I can get the value for each month of the year.
models.py
class Ask(models.Model):
team = models.CharField(max_length=255)
date = models.DateField()
In team are only three possible values: A, B, C.
How do I calculate how many times a month was added individual team?
I would like to generate a report for each year.
Upvotes: 4
Views: 699
Reputation: 4320
I suggest you to add month
field to your model and pass there month on save
. This is help you to run this:
from django.db.models import Count
Ask.objects.filter(date__year='2013').values('month', 'team').annotate(total=Count('team'))
Other method is to use extra parameter and extract month from date field:
from django.db.models import Count
Ask.objects.filter(date__year='2013').extra(select={'month': "EXTRACT(month FROM date)"}).values('month', 'team').annotate(Count('team'))
but EXTRACT method is database dependent and for example dont`t work in SQLite
Upvotes: 3