Django: calculating multiple values of over a set of children

Viewed 19

I'm writing the management interface for a competition, and have the following models:

class Heat(models.Model):
    current_heat = models.IntegerField()
    track = models.ForeignKey(Track, on_delete=models.PROTECT)
    previous_heat = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)
    start_date = models.DateTimeField('Start Date', default=timezone.now)
    end_date = models.DateTimeField('End Date', null=True, blank=True)

class Game(models.Model):
    heat = models.ManyToManyField(Heat)
    format = models.TextField(max_length=20, default="V")
    player1 = models.ForeignKey(User, on_delete=models.PROTECT, related_name='player1')
    player2 = models.ForeignKey(User, on_delete=models.PROTECT, related_name='player2')
    winner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='winner', null=True, blank=True)

Now I want to get the standings for a specific heat. In other words, I want to get all unique players in that heat, together with how many wins they got (thus where they are in Game.winner), and how many losses they got (thus where Game.winner is not that player but it is not Null, and they are in either Game.player1 or Game.player2.)

Ideally, this should then even be ordered by wins (descending), losses (ascending).

I've been looking through the aggregation docs from Django, but I don't see how to start since I need to get so many different things all combined.

0 Answers
Related