Hello ,
I am trying to query database for value occurrence count the sum per day and have result for the last 7 days per each day .
the value is pass_event_type and the date reference in model is event_stamp
my model as follows
class PassEvents(models.Model):
pass_event_type = models.CharField(max_length=200)
event_stamp = models.DateTimeField(auto_now_add=True)
user_related_event = models.CharField(max_length=200)
ip_source = models.CharField(max_length=16)
user_browser = models.CharField(max_length=200)
user_os = models.CharField(max_length=200)
user_agent = models.CharField(max_length=200)
user_reset_reason = models.TextField(max_length=500,default=None, null=True, blank=True)
def __str__(self):
return self.user_related_event
The query i tried in views is as follows
from django.db.models.functions import (
ExtractDay, ExtractMonth, ExtractQuarter, ExtractWeek,
ExtractIsoWeekDay, ExtractWeekDay, ExtractIsoYear, ExtractYear,)
from django.db.models import Count, Sum
context[‘change_pass_line’] = PassEvents.objects.annotate(day=ExtractWeekDay(‘event_stamp’)).values(‘event_stamp’).annotate(count=Count(pass_event_type__exact=‘change password’)).values(‘day’,‘count’)
Please advice
Thanks