I have the following two models:
models.py
class Model_Item(models.Model):
item_name = models.CharField(max_length = 100, null = False, blank = False, unique = True)
def __unicode__(self):
return self.item_name
class Model_ItemTransaction(models.Model):
item = models.ForeignKey(Model_Item, to_field = "item_name")
item_sold = models.FloatField(null = True, blank = True)
def __unicode__(self):
return self.item
With this listview:
views.py
class View_Item(ListView):
def get_queryset(self):
queryset = Model_Item.objects.all()
queryset = queryset.annotate(
sum_ = Sum(When(model_itemtransaction__item_sold))
)
return queryset
And my goal is to get the total value of "item_sold" for each item, and display this value on Listview. How can I make this work?
I have been looking at the conditional expression and this stackoverflow post, but to no avail.