2👍
✅
Just filter on the time_of_visit
and it should work fine:
one_hour_ago = datetime.now()-timedelta(hours=1)
recent_groups = Group.objects.filter(grouptraffic__time_of_visit>=one_hour_ago)
visitors = recent_groups.annotate(views=Count('grouptraffic__visitor', distinct=True))
Then get all of the older groups, with an extra
field for the empty views:
older_groups = Group.objects.filter(grouptraffic__time_of_visit < one_hour_ago).extra(select={'visits':0})
Then concatenate them together with a pipe:
all_groups = visitors | older_groups
Source:stackexchange.com