2011-09-20 62 views
1
Equipment.objects.all() 
total = Equipment.objects.aggregate(price_sum=Sum('price')) 
total_price = total['price_sum'] 

Django新手在这里。我有一个叫做total_price的小数变数。我想要做的就是将价值乘以增值税。现在我的模特里已经有一个可以存储增值税的增值税字段。我希望能够将VAT字段与total_price相乘。在增值税领域,它的工作就是存储一个增值税。我想将total_price乘以增值税

class Equipment(models.Model): 
    price = models.DecimalField(max_digits = 12, decimal_places=2) 
    vat = models.ForeignKey(VAT) 
+0

'schneck''s answer does not work。 见[这里]我的答案[1] [1]:http://stackoverflow.com/questions/12165636/django-aggregation-summation-of-multiplication-of-two-fields/19888120 #19888120 – sha256

回答

0

没有测试过,做这样的工作?

Equipment.objects.aggregate(total=Sum(F('price') * F('vat__<yourfield>'))) 
+0

对不起,但什么是F? –

+0

请看这里:https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model – schneck

+0

不,不起作用 – Ron