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

장고 초보자는 여기에 있습니다. 나는 total_price라는 십진수를 가지고있다. 내가 원하는 것은 VAT 값으로 값을 곱하는 것입니다. 이제 이미 내 모델에 VAT를 저장할 수있는 VAT 필드가 이미 있습니다. 해당 부가가치세 필드에 total_price를 곱하면됩니다. 부가 가치세 필드를 사용하면 하나의 부가 가치세를 저장하는 것만으로 충분합니다.total_price에 vat를 곱하십시오.

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

'schneck'의 대답은 작동하지 않습니다. 는 [여기] 내 대답을 참조 [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