I want to build a simple hot questions list using Django. I have a function that evaluates "hotness" of each question based on some arguments.
Function looks similar to this (full function here)
def hot(ups, downs, date): # Do something here.. return hotness
My models for question and vote models (relevant part)
class Question(models.Model): title = models.CharField(max_length=150) body = models.TextField() created_at = models.DateTimeField(auto_now_add=True) class Vote(models.Model): question = models.ForeignKey(Question, related_name='questions_votes') delta = models.IntegerField(default=0)
delta attribute is either positive or negative. The hot function receives number of positive votes and number of negative votes and creation date of question.
I've tried something like this, but it isn't working.
questions = Question.objects.annotate(hotness=hot(question_votes.filter(delta, > 0),question_votes.filter(delta < 0), 'created_at')).order_by('hotness')
The error I'm getting is:
global name 'question_votes' is not defined
I understand the error, but I don't the correct way of doing this.