abhiabhi94 / dj-hitcount

Basic app that allows you to track the number of hits/views for a particular object.
MIT License
2 stars 0 forks source link

Sort objects by period #7

Closed riquedev closed 2 years ago

riquedev commented 3 years ago

I'm having trouble sorting the objects with the highest hits in a given period, I can't come up with a way to do it. Currently I filter only the most "viewed" of the entire period, I use the syntax below:

ctype = ContentType.objects.get_for_model(News)
HitCount = get_hitcount_model()
hit_count = HitCount.objects.filter(content_type=ctype).order_by('-hits').values_list('object_pk')
popular_news = News.objects.filter(pk__in=hit_count)
abhiabhi94 commented 3 years ago

There is a method hits_in_last for the HitCount model. I think if you can find a way to use this as a way to annotate the hitcount objects, you should be able to achieve what you want.

Please let me know if this doesn't work or you happen to figure out a better way.