title: Django annotating with a first element of a related queryset
body: <h3>Problem</h3>
  
  <p>I am creating a database model for a simple forum. Users should be able to create threads, add posts and post an image with their post.</p>
  
  <p>In a view I would like to display all threads and:</p>
  
  <ul>
  <li>get the fields of the first post in the thread to show a part of the post/creation date etc (including an optional image)</li>
  <li>get the time of the last post in the thread</li>
  <li>count posts in a thread</li>
  <li>count the images in a thread</li>
  </ul>
  
  <p>I believe this is not really possible without executing <code>n</code> queries for <code>n</code> threads, so the real question is how to redesign the database to make that possible.</p>
  
  <pre><code>class Thread(models.Model):
      sticky = models.BooleanField()
      ...
  
  class Post(models.Model):
      thread = models.ForeignKey('Thread')
      image = models.OneToOneField('Image', null=True, blank=True, default=None)
      date = models.DateTimeField()
      ...
  
  class Image(models.Model):
      image = models.ImageField(...)
      ...
  </code></pre>
  
  <h3>My partial solution</h3>
  
  <p>At this point I know how to count posts and images but I have no idea how to fetch the first post at the same time. I thought about adding additional field in the <code>Thread</code> model linking to the first <code>Post</code>.</p>
  
  <p>My query which forces me to download first posts separately:</p>
  
  <pre><code>Thread.objects.annotate(
      replies=Count('post'),
      images=Count('post__image'),
      last_reply=Max('post_date')
  )
  </code></pre>