[Answered ]-How to calculate the sum of a property

2👍

Maybe use F class

from django.db.models import F
total_no_modules = plants.aggregate(sum=Sum(F('nominal_power')*1000-F('module_nominal_power')))

0👍

@Marcin you are correct but I was looking for this:

def home(request):
        plants = Plant.objects.filter(user=request.user)
        total_power = plants.aggregate(sum=Sum('nominal_power'))
        total_no_modules = 0
        for plant in plants
            total_no_modules = total_no_modules + plant.no_modules
        template = 'data/home.html'
        context = {'plants':plants,
                   'total_power':total_power,
                   'total_no_modules':total_no_modules}
        return render(request, template, context)
👤George

Leave a comment