Question

I'm trying to obtain a list of articles and order them by their popularity over time. For example, older articles should rank lower even if they have a higher number of views.

In order to do this each article has a view count and a posted date. I'm guessing the simplest way would be to divide the article view count by the date posted... something like:

(view_count+comment_count) / date_posted = trend_score

I'm trying to understand if this is possible with the Django ORM, even if it is raw SQL? Would appreciate any help.

Was it helpful?

Solution

I guess the simpliest and most effective way to do is to add a trend_score field to your model and update it when the model is saved (you neeed to save the model anyways if you have a view count/comment count on it). Then you can easily filter by this field. You can fore sure do it somehow with SQL, but if you have to update the values you need to update already within your model, calculate also the score upon saving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top