How to aggregate over a single queryset in Django?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:37:00

问题


Short description: given a queryset myQueryset, how do I select max("myfield") without actually retrieving all rows and doing max in python?

The best I can think of is max([r["myfield"] for r in myQueryset.values("myfield")]), which isn't very good if there are millions of rows.

Long description: Say I have two models in my Django app, City and Country. City has a foreign key field to Country:

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')

This means that a Country instance has .cities available. Let's say I now want to write a method for Country called highest_city_population that returns the population of the largest city. Coming from a LINQ background, my natural instinct is to try myCountry.cities.max('population') or something like that, but this isn't possible.


回答1:


Use Aggregation (new in Django 1.1). You use it like this:

>>> from django.db.models import Max
>>> City.objects.all().aggregate(Max('population'))
{'population__max': 28025000}

To get the highest population of a City for each Country, I think you could do something like this:

>>> from django.db.models import Max
>>> Country.objects.annotate(highest_city_population = Max('city__population'))


来源:https://stackoverflow.com/questions/2087670/how-to-aggregate-over-a-single-queryset-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!