我有一个对象列表如何运行查询以给出字段的最大值:
我正在使用这段代码:
def get_best_argument(self):
try:
arg = self.argument_set.order_by('-rating')[0].details
except IndexError:
return 'no posts'
return arg
评级是一个整数
我有一个对象列表如何运行查询以给出字段的最大值:
我正在使用这段代码:
def get_best_argument(self):
try:
arg = self.argument_set.order_by('-rating')[0].details
except IndexError:
return 'no posts'
return arg
评级是一个整数
看到这个。您的代码将类似于以下内容:
from django.db.models import Max
# Generates a "SELECT MAX..." query
Argument.objects.aggregate(Max('rating')) # {'rating__max': 5}
您还可以在现有查询集上使用它:
from django.db.models import Max
args = Argument.objects.filter(name='foo') # or whatever arbitrary queryset
args.aggregate(Max('rating')) # {'rating__max': 5}
如果您需要包含此最大值的模型实例,那么您发布的代码可能是最好的方法:
arg = args.order_by('-rating')[0]
请注意,如果查询集为空,即没有参数匹配查询(因为该[0]
部分将引发IndexError
),这将出错。如果您想避免这种行为,而只是None
在这种情况下返回,请使用.first()
:
arg = args.order_by('-rating').first() # may return None
Django 也有 ' latest(field_name = None) ' 函数,可以找到最新的(最大值)条目。它不仅适用于日期字段,还适用于字符串和整数。
您可以在调用该函数时提供字段名称:
max_rated_entry = YourModel.objects.latest('rating')
return max_rated_entry.details
或者您已经可以在模型元数据中给出该字段名称:
from django.db import models
class YourModel(models.Model):
#your class definition
class Meta:
get_latest_by = 'rating'
现在您可以不带任何参数调用“latest()”:
max_rated_entry = YourModel.objects.latest()
return max_rated_entry.details
我已经为我的项目测试了这个,它在 O(n) 时间内找到了最大值/最小值:
from django.db.models import Max
# Find the maximum value of the rating and then get the record with that rating.
# Notice the double underscores in rating__max
max_rating = App.objects.aggregate(Max('rating'))['rating__max']
return App.objects.get(rating=max_rating)
这可以保证让您有效地获得最大元素之一,而不是对整个表进行排序并获得顶部(大约 O(n*logn))。
如果您还想获得一个值,而不是None
表为空(例如 0),请Max
结合Coalesce:
from django.db.models import Max, Value
from django.db.models.functions import Coalesce
max_rating = SomeModel.objects.aggregate(
max_rating=Coalesce(Max('rating'), Value(0))
)['max_rating']
溶胶 01:
from .models import MyMODEL
max_rating = MyMODEL.objects.order_by('-rating').first()
溶胶 02:
from django.db.models import Max
from .models import MyMODEL
max_rating = MyMODEL.objects.aggregate(Max('rating'))
如果您想要随机评论,可能会改进关于@Raydel Miranda 评论的@afahim 答案。如果你想要全部,那么只使用过滤器
from django.db.models import Max
# Find the maximum value of the rating and then get the record with that rating.
# Notice the double underscores in rating__max
max_rating = App.objects.aggregate(Max('rating'))['rating__max']
return App.objects.filter(rating=max_rating).first()