title: Django model get unique foreign key rows based on other column max value
body: <p>I'm new to Django, and I'm trying to create a simple application that keep track on different server configurations in a SQlite database. I've created 2 database models:</p>
  
  <pre><code>from django.db import models
  
  class Server(models.Model):
      name = models.CharField(max_length=250)
  
  class Config(models.Model):
      server = models.ForeignKey(Server, on_delete=models.CASCADE)
      configuration = models.CharField(max_length=250)
      config_version = models.IntegerField()
  </code></pre>
  
  <p>Here are the 2 models sample data:</p>
  
  <pre><code>Server:
  | id     | name    |
  | ------ | ------  |
  | 1      | Server1 |
  | 2      | Server2 |
  | 3      | Server3 |
  
  Config:
  | id     | configuration | config_version | server |
  | ------ | ------------- | -------------- | ------ |
  | 1      | srv1_cfg1     | 1              | 1      |
  | 2      | srv2_cfg1     | 1              | 2      |
  | 3      | srv2_cfg2     | 2              | 2      |
  | 4      | srv2_cfg3     | 3              | 2      |
  | 5      | srv3_cfg1     | 1              | 3      |
  | 6      | srv1_cfg2     | 2              | 1      |
  | 7      | srv1_cfg3     | 3              | 1      |
  </code></pre>
  
  <p>I would like to query the Config table, and get only rows with the maximum value of "config_version" field for each server id, like:</p>
  
  <pre><code>Desired result:
  | id     | configuration | config_version | serverid | servername |
  | ------ | ------------- | -------------- | -------- | ---------- |
  | 4      | srv2_cfg3     | 3              | 2        | Server2    |
  | 5      | srv3_cfg1     | 1              | 3        | Server3    |
  | 7      | srv1_cfg3     | 3              | 1        | Server1    |
  </code></pre>
  
  <p>I've tried many different options to construct the correct query, but so far I cannot get what I want. My best result is to query the Server table: </p>
  
  <pre><code>Server.objects.annotate(maxver=Max('config__config_version'))
  </code></pre>
  
  <p>But it seems I cannot get access to the Config table objects, so I guess I need to query the Config table with some filtering?</p>
  
  <p>I can do this with a raw SQL query, but I would strongly prefer to do it the "Django" way. Any help will be much appreciated.</p>