Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
Incremental totals in Django Model

I have a table as below: Example Table

and I have a model as

class Hours(models.Model):
      name = model.Charfield(max_length =200)
      hours = DecimalField(max_digits = 6, decimal_places=2)

     def total_hours(self):
        queryset = Hours.objects.aggregate(total_hours=models.Sum('hours'))
        return queryset['total_hours']

now when I run the code I get all records have the to 37 instead of them having a running total.

where did I miss a step?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Whenever you call total_hours its getting the sum of hours over all Hours objects. You'd need something like:

Hours.objects.filter(id__lte=self.id).aggregate(total_hours=models.Sum('hours')) 

for a running total

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda