title: Django custom annotation function
body: <p>I want to build a simple hot questions list using Django. I have a <a href="https://web.archive.org/web/20150526231733/http://amix.dk/blog/post/19588" rel="nofollow noreferrer">function</a> that evaluates &quot;<strong>hotness</strong>&quot; of each question based on some arguments.</p>
  <p>Function looks similar to this (<a href="https://web.archive.org/web/20150526231733/http://amix.dk/blog/post/19588" rel="nofollow noreferrer">full function here</a>)</p>
  <pre><code>def hot(ups, downs, date):
      # Do something here..
      return hotness
  </code></pre>
  <p>My models for question and vote models (relevant part)</p>
  <pre><code>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)
  </code></pre>
  <p>Now, the <code>delta attribute</code> is either positive or negative. The hot function receives number of positive votes and number of negative votes and creation date of question.</p>
  <p>I've tried something like this, but it isn't working.</p>
  <pre><code> questions = Question.objects.annotate(hotness=hot(question_votes.filter(delta, &gt; 0),question_votes.filter(delta &lt; 0), 'created_at')).order_by('hotness')
  </code></pre>
  <p>The error I'm getting is: <code>global name 'question_votes' is not defined</code><br />
  I understand the error, but I don't the correct way of doing this.</p>