0

I've started building an app in Django that will require multi-criteria weighted ratings. My issue is the exact same as this one from a few years ago, which unfortunately did not have any answer.

If anybody out there can give me some hints on how to make multi-criteria weighted ratings in Django, I would love to hear about it! I'm pretty new to the whole Django/Python world and come from a PHP background.

Thanks a lot!

PS: I did not put too much info in this thread, because the problem is already described on the other topic here. It's exactly the same thing.

Alb Dum
  • 1,071
  • 3
  • 10
  • 25

2 Answers2

1

The easiest solution for the weighted rating would be to add an additional field and populate it automatically in a custom save method on the rating object.

As far as getting the overall average rating goes, you can either calculate it dynamically every time it's needed (easiest/most accurate method) using Django's aggregation queries, or you can try to maintain an average rating field that is updated each time a rating is added/deleted/updated (the more performant method if you're running a read-heavy service). See my answer here for more details on implementing these (using sums rather than averages) and more detail on the tradeoffs.

Community
  • 1
  • 1
Michael C. O'Connor
  • 9,186
  • 3
  • 34
  • 49
  • Thanks a lot for this answer. That's how I implemented it in PHP, with a cron task running regularly and calculating the weighted scores. The calculation on the go seems unrealistic in my case, as I am expecting several thousands of possible combinations. So I'll check this aggregation thing, but I might go for the other method. I was just wondering if there was some sort of a Django plugin or anything like this that could help me. Thanks! – Alb Dum Aug 16 '12 at 13:13
0

If you do not want to create a separate field that you need to maintain by overriding the save method, you need to follow this slightly more complex approach: Using .aggregate() on a value introduced using .extra(select={...}) in a Django Query?

Community
  • 1
  • 1
schacki
  • 9,176
  • 4
  • 23
  • 28