Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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
Python 聚合前的算术运算_Python_Django_Orm_Aggregate - Fatal编程技术网

Python 聚合前的算术运算

Python 聚合前的算术运算,python,django,orm,aggregate,Python,Django,Orm,Aggregate,我的models.py中有此表: class Vendas(models.Model): ''' Contem informacoes sobre venda realizadas. ''' purchaser_name = models.CharField(max_length=100) item_description = models.CharField(max_length=100) item_price = models.DecimalField(ma

我的models.py中有此表:

class Vendas(models.Model):
    ''' Contem informacoes sobre venda realizadas. '''

    purchaser_name = models.CharField(max_length=100)
    item_description = models.CharField(max_length=100)
    item_price = models.DecimalField(max_digits=3, decimal_places=1)
    purchase_count = models.IntegerField()
    merchant_address = models.CharField(max_length=100)
    merchant_name = models.CharField(max_length=100)
这种观点:

def success(request):
    ''' Pagina de retorno para quando o arquivo eh carregado com sucesso! '''

    total = ....
    return render(request, "file_upload/success.html", locals())
在我看来,我需要将
item\u price
purchase\u count
两个字段相乘,然后将它们相乘

在SQL中,它类似于从Vendas中选择SUM(项目价格*购买数量)但使用
对象。原始
也不起作用

有人能帮我吗


提前谢谢

我会为每个项目添加一个计算属性,然后在视图中对它们进行求和(如果您也需要)

class Vendas(models.Model):
    ''' Contem informacoes sobre venda realizadas. '''

    purchaser_name = models.CharField(max_length=100)
    item_description = models.CharField(max_length=100)
    item_price = models.DecimalField(max_digits=3, decimal_places=1)
    purchase_count = models.IntegerField()
    merchant_address = models.CharField(max_length=100)
    merchant_name = models.CharField(max_length=100)

    def _this_total(self)
        return self.item_price*self.purchase_count
    this_total = property(_this_total)

def success(request):
    ''' Pagina de retorno para quando o arquivo eh carregado com sucesso! '''
    objects = <your query for objects here>
    total = sum([i.this_total for i in objects])
    return render(request, "file_upload/success.html", locals())
classvendas(models.Model):
“我的信息是真实的”
买方名称=型号.CharField(最大长度=100)
项目描述=型号.字符字段(最大长度=100)
项目价格=型号。小数字段(最大位数=3,小数位数=1)
purchase\u count=models.IntegerField()
商户地址=models.CharField(最大长度=100)
商户名称=models.CharField(最大长度=100)
定义此总数(自身)
退货自购商品价格*自购数量
this\u total=属性(\u this\u total)
def成功(请求):
“‘我们的事业成功了!’
对象=
总计=总和([i.对象中i的总计])
返回渲染(请求“file\u upload/success.html”,locals())
使用和:

例如:

>>> Vendas.objects.create(item_price='5.0', purchase_count=1)
<Vendas: Vendas object>
>>> Vendas.objects.create(item_price='15.0', purchase_count=5)
<Vendas: Vendas object>
>>> Vendas.objects.extra(select={
...     'price': 'sum(item_price * purchase_count)'
... }).values_list('price', flat=True)[0]
80
>Vendas.objects.create(商品价格=5.0',采购数量=1)
>>>Vendas.objects.create(item_price='15.0',purchase_count=5)
>>>Vendas.objects.extra(选择={
…价格:'总额(项目价格*采购数量)'
…}).values_list('price',flat=True)[0]
80

谢谢!它起作用了,但我实际上不知道
.extra
对象的各个部分到底是什么意思。@LucasRezende,你是否按照答案中的链接进行了操作?你会发现文档中包含了关于
extra
的详细信息。实际上,我没有注意到它是一个链接,但正如我现在所知,我会仔细查看它以了解详细信息。再次感谢你的帮助!
>>> Vendas.objects.create(item_price='5.0', purchase_count=1)
<Vendas: Vendas object>
>>> Vendas.objects.create(item_price='15.0', purchase_count=5)
<Vendas: Vendas object>
>>> Vendas.objects.extra(select={
...     'price': 'sum(item_price * purchase_count)'
... }).values_list('price', flat=True)[0]
80