Python Django通过ForeignField相关的表进行聚合

Python Django通过ForeignField相关的表进行聚合,python,django-models,django-orm,django-3.0,Python,Django Models,Django Orm,Django 3.0,我想将指定订单的总价作为订单模型的总价(self),这样它就可以将订单项的金额乘以它们的价格求和。我无法通过pr_id字段与产品表关联以获取产品价格 Traceback (most recent call last): File "<console>", line 1, in <module> File "D:\python\projects\pipingapi\restapi\models.py", line 75, in total_price ord

我想将指定订单的总价作为订单模型的总价(self),这样它就可以将订单项的金额乘以它们的价格求和。我无法通过pr_id字段与产品表关联以获取产品价格

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\python\projects\pipingapi\restapi\models.py", line 75, in total_price
    order_price = self.items_in_order.all().aggregate(price=Sum(F('amount') * F('pr_id.pr_price')))
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py", line 376, in aggregate
    query.add_annotation(aggregate_expr, alias, is_summary=True)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1000, in add_annotation
    summarize=is_summary)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\aggregates.py", line 47, in resolve_expression
    c = super().resolve_expression(query, allow_joins, reuse, summarize)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\expressions.py", line 600, in resolve_expression
    c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\expressions.py", line 447, in resolve_expression
    c.rhs = c.rhs.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\expressions.py", line 511, in resolve_expression
    return query.resolve_ref(self.name, allow_joins, reuse, summarize, simple_col)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1601, in resolve_ref
    join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse)
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1504, in setup_joins
    names[:pivot], opts, allow_many, fail_on_missing=True,
  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\sql\query.py", line 1420, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'pr_id.pr_price' into field. Choices are: amount, id, or_id, or_id_id, pr_id, pr_id_id

一种是使用双下划线(
\uuuu
)来查看关系,因此:

order_price = self.items_in_order.aggregate(
    price=Sum(
        ExpressionWrapper(F('amount'), output_field=DecimalField(max_digits=10, decimal_places=2)) *
        F('pr_id__pr_price')
    )
)
order\u price=self.items\u in\u order.aggregate(
价格=总和(
ExpressionWrapper(F('amount'),输出字段=DecimalField(最大位数=10,小数位数=2))*
F('pr_id__pr_price')
)

)
使用双下划线(
\uuu
)查看关系,因此:

order_price = self.items_in_order.aggregate(
    price=Sum(
        ExpressionWrapper(F('amount'), output_field=DecimalField(max_digits=10, decimal_places=2)) *
        F('pr_id__pr_price')
    )
)
order\u price=self.items\u in\u order.aggregate(
价格=总和(
ExpressionWrapper(F('amount'),输出字段=DecimalField(最大位数=10,小数位数=2))*
F('pr_id__pr_price')
)
)
class Product(models.Model):
    PIPES = 'PI'
    FITTINGS = 'FI'
    VALVES = 'VA'
    WELD_AND_THREAD = 'WT'
    CATEGORY_CHOICES = [(PIPES, 'Pipes'),
                        (FITTINGS, 'Fittings'),
                        (VALVES, 'Valves'),
                        (WELD_AND_THREAD, 'Welds and threads')]

    pr_id = models.AutoField(primary_key=True)
    pr_name = models.CharField(max_length=50)
    pr_cat = models.CharField(max_length=2, choices=CATEGORY_CHOICES)
    pr_price = models.DecimalField(max_digits=8, decimal_places=2)
    pr_sup = models.ForeignKey(Supplier, on_delete=models.PROTECT)

    def __str__(self):
        return self.pr_name

order_price = self.items_in_order.aggregate(
    price=Sum(
        ExpressionWrapper(F('amount'), output_field=DecimalField(max_digits=10, decimal_places=2)) *
        F('pr_id__pr_price')
    )
)