【发布时间】:2021-07-24 08:22:39
【问题描述】:
我正在尝试计算食谱的总价格。为了优化数据库查询,我尝试使用 Django 的 ORM 功能来执行最少的请求。
我的models.py是这样的:
class BaseRecipe(models.Model):
title = models.CharField(_('Base recipe title'), max_length=255,
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE, related_name='user_base_recipes')
class Meta:
ordering = ['title']
def __str__(self):
return self.title
class IngredientBaseRecipe(models.Model):
base_recipe = models.ForeignKey(BaseRecipe, on_delete=models.CASCADE, related_name='ingredients')
name = models.CharField(_('Name'), max_length=255)
products = models.ManyToManyField(Product)
quantity = models.FloatField(_('Quantity'), default=0.0)
class Meta:
ordering = ['-id']
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(_('Name'), max_length=255, help_text=_('Product name'))
price = models.FloatField(_('Sale price'), default=0)
class Meta:
ordering = ['name', ]
indexes = [models.Index(fields=['name',]), ]
然后在我的 Viewset 中,我试图获取 BaseRecipes 查询集,其中包含一个显示成分价格总和的带注释字段。我得到了获取原料价格的目的,但我试图在 BaseRecipe 查询集中对它们求和:
min_price = (
Product.objects.filter(name=OuterRef('name'))
.annotate(min_price=Min('price'))
.values('min_price')
)
ingredients_price = (
IngredientBaseRecipe.objects
.filter(base_recipe=OuterRef('id'))
.annotate(price=Subquery(min_price))
.order_by()
.annotate(total=Sum(F('price') * F('quantity')))
.values('total')
)
queryset = BaseRecipe.objects.filter(user=self.request.user) \
.annotate(cost=Sum(ingredients_price))
return queryset
非常感谢您的帮助!
【问题讨论】:
标签: django django-models django-orm