title: How to aggregate over a single queryset in Django?
body: <p><strong>Short description</strong>: given a queryset <code>myQueryset</code>, how do I select <code>max("myfield")</code> without actually retrieving all rows and doing <code>max</code> in python?</p>
  
  <p>The best I can think of is <code>max([r["myfield"] for r in myQueryset.values("myfield")])</code>, which isn't very good if there are millions of rows.</p>
  
  <p><strong>Long description</strong>: Say I have two models in my Django app, City and Country. City has a foreign key field to Country:</p>
  
  <pre><code>class Country(models.Model):
      name = models.CharField(max_length = 256)
  
  class City(models.Model):
      name = models.CharField(max_length = 256)
      population = models.IntegerField()
      country = models.ForeignKey(Country, related_name = 'cities')
  </code></pre>
  
  <p>This means that a Country instance has <code>.cities</code> available. Let's say I now want to write a method for Country called <code>highest_city_population</code> that returns the population of the largest city. Coming from a LINQ background, my natural instinct is to try <code>myCountry.cities.max('population')</code> or something like that, but this isn't possible.</p>