0

Let's assume we have the following two models:

class Player(Model):
    alliance = ForeignKey("Alliance")
    points = PositiveIntegerField()

class Alliance(Model):
    points = PositiveIntegerField()

Every player and every alliance have a specific amount of points. Total alliance points are count alliance.points + Sum(player_set__points).

What I want to do is to fetch all the alliances ordered by the amount of total points. The problem is that I do not seem to be able to do Sum + Sum in aggregation.

aemdy
  • 3,702
  • 6
  • 34
  • 49

1 Answers1

1

You can denormalize your DB. Add a field sum on an Alliance, an update it on post_save and post_delete of Alliance and Player. So you'll have ready-to-use value and sort on it easily.

ilvar
  • 5,718
  • 1
  • 20
  • 17
  • Well, prestige of the player changes very often and because of that I want to calculate the sum on the fly. It seems to be impossible, doesn't it? – aemdy Apr 24 '12 at 10:04
  • Not impossible, you always can have raw sql. But much less elegant. – ilvar Apr 25 '12 at 04:02