python-3.xdjangodjango-viewsdjango-3.0django-aggregation

Aggregate Sum shows total for all members


When i add aggregate function on my get_context_data it shows the total for all members and not according to there ID. Thank You

ItemListView

class ItemListView(ListView):
    model = HBTYItem
    template_name = "accounts/modals/nomodal/todo_list.html"
    paginate_by = 2
    ordering = ['id']

    def get_queryset(self):
        return HBTYItem.objects.filter(hbty_cust_id=self.kwargs["list_id"])

    def get_context_data(self):
        context = super().get_context_data()
        context['t_sum'] = HBTYItem.objects.aggregate(Sum('price'))
        context["hbty_list"] = HBTYList.objects.get(id=self.kwargs["list_id"])
        return context

Solution

  • If you have user filed in HBTVItem you can use:

    HBTYItem.objects.filter(user=self.request.user).aggregate(Sum('price'))
    

    Or you can apply filter on any field you want it