I have three models (Django 1.6.5):
class Vote(models.Model):
voter = models.ForeignKey(UserSettings)
answer = models.ForeignKey(Answer)
rating = models.IntegerField()
class Answer(models.Model):
content = models.CharField(max_length=255)
class UserSettings(models.Model):
user = models.OneToOneField(User, related_name='settings')
weight = models.FloatField(default=1.0)
Basically, a user (voter) can vote for an answer by giving a rating. I know how to sum the ratings by answer:
Vote.objects.all().values('answer').annotate(score=Sum('rating'))
The only subtlety is that each voter has a weight (all voters are not equal!) and I want to sum each product rating*weight. I know (from here) that something like that can be done:
Sum('id',field="field1*field2")
and it would work well if my 2 fields are in the same model but it doesn't work if they are not. In other words, command:
Vote.objects.all().values('answer').annotate(score=Sum('id',field="rating*voter__weight"))
does not work. Any help greatly appreciated!