Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How to calculate the sum of a property

Hello everyone here is my question:

models.py

class Plant(models.Model)
   nominal_power = models.PositiveIntegerField()
   module_nominal_power= models.PositiveIntegerField()

@property
    def no_modules(self):
       return round(self.nominal_power*1000/self.module_nominal_power)

views.py

def home(request):
    plants = Plant.objects.filter(user=request.user)
    total_power = plants.aggregate(sum=Sum('nominal_power'))
    total_no_modules = plants.aggregate(sum=Sum('no_modules'))['sum']
    template = 'data/home.html'
    context = {'plants':plants,
               'total_power':total_power,
               'total_no_modules':total_no_modules}
    return render(request, template, context)

And I get the error Cannot resolve keyword 'no_modules' into field.Cannot resolve keyword 'no_modules' into field.

I undestand the meaning of the failure but how can I get the total number?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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')))
over 4 years ago · Santiago Trujillo Report

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!