Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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 “dict”对象没有属性“order\u by”django_Python_Django_Aggregate - Fatal编程技术网

Python “dict”对象没有属性“order\u by”django

Python “dict”对象没有属性“order\u by”django,python,django,aggregate,Python,Django,Aggregate,我想返回多个字段的数据,并且我已经使用聚合进行了一些计算,现在我还需要返回产品 这是我的模特 class CustomerInvoice(models.Model): customer = models.CharField(max_length=50) items = models.ManyToManyField(Product,through='ProductSelecte') date = models.DateTimeField(auto_now_add=True)

我想返回多个字段的数据,并且我已经使用聚合进行了一些计算,现在我还需要返回产品

这是我的模特

class CustomerInvoice(models.Model):
    customer = models.CharField(max_length=50)
    items = models.ManyToManyField(Product,through='ProductSelecte')
    date = models.DateTimeField(auto_now_add=True)

class ProductSelecte(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    products= models.ForeignKey(CustomerInvoice,on_delete=models.CASCADE,related_name='product')
    qnt= models.IntegerField(default=1)
    price = models.IntegerField()
    cash = models.IntegerField()
这是我的问题

context['clients'] = CustomerInvoice.objects.filter(
        customer=self.object.name).aggregate(
            total_order=Sum(F('product__qnt')),
            total_price=Sum(F('product__price')))
我想制作一个表格来跟踪客户的活动:例如,在6月4日,他用3本书买了2支钢笔,在7月5日,他用2本文案买了1支钢笔,我需要这样的结果:3;pen,3;书籍,2本;文案

我知道我应该使用distinct,我不知道为什么没有任何输出{{clients.items.all | join:','}}? 谢谢你的帮助

更新

现在我将查询更改为

context['clients'] = ProductSelecte.objects.filter(
    products__customer=self.object.name).values('product').annotate(quantity=Sum(F('qnt'))).order_by('product')
直到在这里工作很好,但我也需要使用这个总价格

.aggregate(
        total_price=Sum(F('price')))
它引起了这个错误

“dict”对象没有属性“order\u by”


改变你的方法。不要从CustomerVoice转到ProductSelecte,而是从ProductSelecte转到CustomerVoice

ProductSelecte.objects.filter(products__customer=self.object.name)
现在,您从客户处购买的每件商品都有商品名称和数量


现在,如果您真的需要,请在此查询中使用聚合来获取每张发票上的数据

ProductSelecte.objects.filterproducts\uu customer=self.object.name.values'product'.annotatecount=SumF'qnt'我写道,没有测试,因此可能需要检查语法。上面的数据相当于此SQL查询:选择您的应用程序\u ProductSelecte.product\u id,使用yourapp\u productselecte.qnt作为yourapp\u productselecte内部加入yourapp\u CustomerVoice ON yourapp\u productselecte.products\u id=yourapp\u CustomerVoice.id,其中yourapp\u CustomerVoice.customer=val组由yourapp\u productselecte.product_id@art_cs你在使用我提出的第二个问题吗您?ProductSelecte.objects.filterproducts\uuu customer=self.object.name.values'product'.annotatecount=SumF'qnt'@art\u cs乐于帮助:如果您的问题得到解决,请接受答案