我有3个型号:
class Model1(models.Model):
name = models.CharField(max_length=30)
class Model2(models.Model):
model1 = models.ForeignKey(Model1)
class Model3(models.Model):
model2 = models.OneToOneField(Model2)
value = models.IntegerField()
我想获得Model1#model2_set#model3.value
中的最大值。我怎么能这样做?
class Model1(models.Model):
name = models.CharField(max_length=30)
def model2_max_value(self):
model2_set.
# for each item in model2_set
# get model2_set_item.model3.value
# and find max among them
我以为我可以在这里申请aggregate(Max)
,但无法找到方法。
答案 0 :(得分:0)
这样的事情应该有效:
max_value = Model3.objects.filter(model2__model1=self).aggregate(Max('value'))