Annotating Django querysets with a column from the first row of a reverse relation
<p>I have a <code>Review</code> model, and a <code>ReviewScore</code> ForeignKey'd to it, representing a score at a certain point in time. Simplified, it looks like this:</p>

<pre><code>class Review(models.Model):
    # Nothing interesting here.


class ReviewScore(models.Model):
    review = models.ForeignKey(Review)
    date = models.DateField()
    score = models.IntegerField()

    class Meta:
        ordering = ("-date",)
</code></pre>

<p>In my view, I wish to annotate a Review queryset with the value of <code>score</code> from the most recent row in the ReviewScore table (specifically, I do <em>not</em> want an average), so that I can sort by the most recent score given to a Review.</p>

<p>I've tried <code>.annotate(score=F("reviewscore__score"))</code> but this returns multiple rows; thus it returns duplicate <code>Review</code>s, each annotated with a different ReviewScore score, and using <code>distinct</code> can't cure this because Postgres requires that a <code>distinct</code> must match the ORDER BY expression.</p>

<p>Is this possible? Can I somehow filter or limit the results returned the foreign table with <code>.annotate</code>, or otherwise make it only annotate with the first result from the <code>ReviewScore</code> table?</p>