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

170
Vistas
Django, update rate, signals
class Thing (model.Models):
    name = models.CharField(max_length = 222)
    ratee = models.IntegerField(default = 0)
    ...

class Rate(models.Model):
    thing = models.ForeignKey(Thing)
    user = models.ForeignKey(User)
    rate = models.IntegerField()

If user evaluate the thing(give rate in Rate), I want to automatically calculate average and save to ratee in Thing. How to make it?

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

0

If you want to use a signal, you can do the following

from django.dispatch import receiver
from django.db.models.signals import post_save
from django.db.models import Avg
@receiver(post_save, sender=Rate, dispatch_uid="update_rating")
def update_rating(sender, instance, **kwargs):
     # Logic to update the rating goes here, for example...
     avg = Rate.objects.filter(thing=instance.thing) \
                       .aggregate(Avg('rate')).values()[0]
     instance.thing.ratee = avg
     instance.thing.save()

Alternatively, you can override the save method in Rate to do this without signals. That would look more like:

class Rate(models.Model):
    # ...

    def save(self, *args, **kwargs):
        super(Rate, self).save(*args, **kwargs)

        # Logic to update the average rating here
over 4 years ago · Santiago Trujillo Denunciar

0

You can do

class Rate(models.Model):

    def save(self, *args, **kwargs):
        super(Rate, self).save(*args, **kwargs)
        self.thing.ratee = Rate.objects.filter(thing=self.thing).aggregate(Avg('rate'))["rate__avg"]
        self.thing.save()
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