I am building a simple hacker news website and would like some help on how to integrate a ranking algorithm for submitted posts.
I understand that I can rank submitted posts via 'order_by', which I have done for now:
def index(request):
submitted_items = Submission.objects.all().order_by('-votecount')[:20]
However, I would like to make the ranking more intelligent by factoring in the date at which the submission was made.
To give a little bit more context, I used this code block from a tutorial that I found as a base for trying to figure out how to implement such an algorithm:
class HomeView(TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
ctx = super(HomeView, self).get_context_data(**kwargs)
now = timezone.now()
submissions = Link.objects.all()
for submission in submissions:
num_votes = submission.upvotes.count()
num_comments = submission.comment_set.count()
date_diff = now - submission.submitted_on
number_of_days_since_submission = date_diff.days
submission.rank = num_votes + num_comments - number_of_days_since_submission
sorted_submissions = sorted(submissions, key=lambda x: x.rank, reverse=True)
ctx['submissions'] = sorted_submissions
return ctx
But I couldn't get this to work in my own application.
I guess another way to phrase my question is this. Django/Python has a standard order_by() function that allows me to sort data, most simply in an ascending or descending order. Is there a way to create my own unique custom order_by() function that I can use?
Thanks in advance for the help!