Reputation: 391
I have a "startTime" field in my model which contains timestamp.I want to get a list with count of only hour from that stamp.I mean my code should be able convert and get only hour from the timestamp and count the number of occurance of each hour.I tried doing
datetime.datetime.fromtimestamp(int(<timestamp>)).strftime(\'%H\'))
this thing returns only hour from a timestamp but when i tried incorporating it in the queryset..it failed..this is what i tried in my views.py
timehour = InteractionWith.objects \
.values('startTime')\
.annotate(times=Count('datetime.datetime.fromtimestamp(int("startTime")).strftime(\'%H\'))'))
Upvotes: 1
Views: 657
Reputation: 476584
You can annotate the queryset with ExtractHour
[Django-doc]:
from django.db.models import Count
from django.db.models.functions import ExtractHour
timehour = InteractionWith.objects.values(
hour=ExtractHour('startTime')
).annotate(
times=Count('pk')
).order_by('hour')
Upvotes: 1