0
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?

2 Answers2

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
NS0
  • 6,016
  • 1
  • 15
  • 14
  • Doesn't work for me. You're really sure about this solution? I need sum of rate from Rate, but you give sum of ratee from Thing, not from Rate. and what is the product here? instance.product.save() – abcdefghijklmn8 Apr 25 '17 at 03:30
  • Updated answer. – NS0 Apr 25 '17 at 03:33
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()
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • I do not why, but nothing happens with "ratee" when I adding rate to the Rate. Any ideas why? – abcdefghijklmn8 Apr 25 '17 at 06:22
  • Did you override `Thing` save method ? OR check `print Rate.objects.filter(thing=self.thing).aggregate(Avg('rate'))["rate__avg"]` – itzMEonTV Apr 25 '17 at 06:24
  • What do you mean "override Thing save method"? print didn't display anything – abcdefghijklmn8 Apr 25 '17 at 06:32
  • This should be in model.Post your code where saving `Rate`.Are you updating through admin ? `If user evaluate the thing(give rate in Rate)` where is this happening? – itzMEonTV Apr 25 '17 at 06:36
  • actually through admin. – abcdefghijklmn8 Apr 25 '17 at 06:37
  • Are you using any custom admin? How you are registering this model in admin.py? Check whether `save` method is executing by printing something inside. Check all values are expected by printing inside `save` method above.`save` method should be inside class `Rate`(check indendation). – itzMEonTV Apr 25 '17 at 06:39
  • Before I tried add this through SQLite Manager mostly, but now I tried through the admin model, and this WORK. Thank you very much. Maybe you know, why through the sqlite manager was wrong and not working? – abcdefghijklmn8 Apr 25 '17 at 06:42