'calculating percentages on a field in Django
I have the model below and I am trying to calculate the percentages of product quantities. Any help would be greatly appreciated. Thanks
model.py
class Stock(models.Model):
date = models.DateField(default=now)
product = models.CharField(max_length=100, null=True, unique=True)
quantity = models.IntegerField(default='0')
view.py
total= Stock.objects.aggregate(total_vote=Sum('quantity'))
per = Stock.objects.values('quantity')
percentage = [
{'quantity': p['quantity'], 'percentage': p['quantity'] * 100 /total}
for p in per
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
