Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django和python,如何从两个不同的模型中获得注释?_Python_Python 3.x_Django_Django Models_Django Views - Fatal编程技术网

Django和python,如何从两个不同的模型中获得注释?

Django和python,如何从两个不同的模型中获得注释?,python,python-3.x,django,django-models,django-views,Python,Python 3.x,Django,Django Models,Django Views,我有以下模型框架: class Subcategory(models.Model): nome=models.CharField() class Order(models.Model): order=models.CharField() class Quantity(models.Model): order=models.ForeignKey(Order) subcategory=models.ForeignKey(Subcategory) quant

我有以下模型框架:

class Subcategory(models.Model):
    nome=models.CharField()

class Order(models.Model):
    order=models.CharField()

class Quantity(models.Model):
    order=models.ForeignKey(Order)
    subcategory=models.ForeignKey(Subcategory)
    quantity=models.DecimalField()

class Price(models.Model):
    order=models.ForeignKey(Order)
    subcategory=models.ForeignKey(Subcategory)
    price=models.DecimalField()
现在,我想获得一个新的值,该值使我能够过滤
子类别
订单
这两个
价格
数量
查询集,并给出它们的摩尔应用。 这是我设置的代码,但我不知道如何获得
price*quantity
操作

cod='1234'
price=dict()
defaults=list(0 for m in range(1))
filter_quantity = list(Quantity.objects.values_list('subcategory__id', flat=True).distinct()).filter(order__order=cod)
    
    for subcategory__id, totals in(Price.objects.filter(
        subcategoty__in=filter_quantity ).values_list('subcategory__id').annotate(totals=ExpressionWrapper(Sum(F('price')),
                output_field=FloatField())).values_list('subcategory__id', 'totals'):
                if subcategory__id not in price.keys():
                    price[subcategory__id ]=list(defaults)
                index=0
                price[subcategory__id][index]=totals
total_costs={'Costs': [sum(t) for t in zip(*price.values())]}

您还可以根据需要对此方法进行更改

def get_order_details(order_code):
    order_details = []
    quantities = Quantity.objects.filter(order__order=order_code)
    prices_queryset = Price.objects.filter(order__order=order_code)

    for quantity in quantities:
        price = prices_queryset.filter(order__order=order_code, subcategory=quantity.subcategory).first()
        if price:
            order_details.append({
                'subcategory_name': quantity.subcategory.nome,
                'quantity': quantity.quantity,
                'unit_price': price.price,
                'total_price': quantity.quantity * price.price
            })

    return {
        'order_code': order_code,
        'details': order_details
    }

因此,您想对数量进行注释吗?是的,我想获得基于子类别和订单填充值的数量*价格的Molti应用,因为订单可以有多个数量和多个价格,因此,没有1-1映射。必须为数量中存在的订单和子类别过滤价格