django - Sort models by number of occurences -
the question has been asked multiple times haven't been able make code works. anyway, let's have blog, blog posts:
class blogpost(models.model): # class doesn't matter author = models.foreignkey(myuser) text = models.textfield()
it happens log visits on each blog post using model:
class activity(models.model): user_id = models.charfield() # log different types of activities, hence choice action_id = models.integerfield(choices=activity_actions) object_id = models.charfield() created_at = models.datetimefield()
what want visitied blog posts since precise date, ordered number of views. i'm doing right (in activity manager):
def get_blogposts_most_viewed_since(self, date): # related activities (blog posts views since date) activities = activity.objects.since(date).filter(action_id=blogpost_view) # blogposts' ids blogpost_ids = events.values('object_id') # blogposts ordered number of views: blogposts = blogpost.objects.filter(id__in=blogpost_ids)\ .annotate(nb_views=count('id')).order_by('-nb_views') return blogposts
this doesn't work. made tests: create 3 blog posts, blogpost_a viewed twice, blogpost_b viewed once , blogpost_c doesn't views. when call get_blogposts_most_viewed_since
blogpost_a , blogpost_b in seems random_order.
i tried debugging with:
for blogpost in blogposts: print blogpost.nb_views
both blogposts (a , b) have nb_views == 1
.
what doing wrong?
Comments
Post a Comment