2017-04-11 68 views
0
class attributes(models.Model): 

     attribute_name = models.CharField(max_length = 200, db_index = True) 
     price_update = models.DecimalField(max_digits = 10, 
             decimal_places = 2) 


class Product(models.Model): 

     attribute = models.ManyToManyField(attributes) 
+2

嗯,有一些车型。你的问题到底是什么? –

+0

属性有许多price_update对象。我想计算每个产品的总价格更新值。 –

回答

1

我想你需要

from django.db.models import Sum 
Product.objects.filter(id=product_id).aggregate(Sum('attribute__price_update')) 
+0

如果OP想要查询集中每个产品的总和,那么'annotate'比'aggregate'更合适。 – Alasdair

+0

好的,谢谢,IT工作 –

+0

你告诉我,我为这个计算写什么观点 –