2011-09-20 60 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