title: How to filter a queryset for the highest value in one field grouped by each value in another field?
body: <p>If I have a model like this</p>
  
  <pre><code>from django.db import models
  
  class Thing(models.Model):
      name = models.CharField(max_length=255, unique = True, null=False)
      version = models.IntegerField(null = False)
      type = models.CharField(max_length=255, null=False)
  
  </code></pre>
  
  <p>and I have a bunch of entries like</p>
  
  <pre><code>Thing.objects.create(name = "foo1", version = 1, type = "A")
  Thing.objects.create(name = "foo2", version = 2, type = "A")
  Thing.objects.create(name = "bar1", version = 1, type = "B")
  Thing.objects.create(name = "bar2", version = 2, type = "B")
  Thing.objects.create(name = "bar3", version = 3, type = "B")
  </code></pre>
  
  <p>How do I write a single Django query that will give me one entry for each of type <code>type</code> with the highest <code>version</code>? So, this result:</p>
  
  <pre><code>Queryset&lt; Thing(name = "foo2", version = 2, type = "A"), 
  Thing(name = "bar3", version = 3, type = "B") &gt;
  </code></pre>
  
  <p>I was looking at the docs here; <a href="https://docs.djangoproject.com/en/3.0/topics/db/aggregation/" rel="nofollow noreferrer">https://docs.djangoproject.com/en/3.0/topics/db/aggregation/</a></p>
  
  <p>And its easy to do something like this;</p>
  
  <pre><code>Thing.objects.aggregate(Max('version'))
  </code></pre>
  
  <p>But this would only give me <code>Thing(name = "bar3", version = 3, type = "B")</code></p>