Reputation: 90
I have views counter (I used django-hitcount)
Models:
class Article(Created, HitCountMixin):
title = models.CharField(max_length=120)
body = RichTextField(null=False)
hit_count_generic = GenericRelation(
MODEL_HITCOUNT, object_id_field='object_pk',
related_query_name='hit_count_generic_relation')
Views:
class AllArticlesListView(ListView):
template_name = 'news/articles_list.html'
model = Article
paginate_by = 5
and I sort articles by number of all article views like this:
def get_queryset(self):
return self.model.objects.all().order_by('-hit_count_generic__hits')
and everything is OK.
Now I want make most viewed in last 10minutes, like:
def get_queryset(self):
return self.model.objects.all().order_by('-hit_count_generic__hits_in_last(minutes=10)')
But it doestn work... still sort articles by all views not views in last 10minutes. How can I do?
Upvotes: 0
Views: 107
Reputation: 2018
You can try this way
import datetime
from django.db.models import Count
def get_queryset(self):
period = datetime.datetime.now() - datetime.timedelta(minutes=10)
return self.model.objects.filter(
hit_count_generic__hits__created__gte=period
).annotate(
counts=models.Count('hit_count_generic__hits')
).order_by('-counts')
Upvotes: 1