0

I have this model:

class ScoringModel(model.Model):
    red_score = models.SmallIntegerField()
    blue_score = models.SmallIntegerField()

I'm trying to sort a query set based on whichever is greater between the two. Now, taking the sum of them wouldn't work because that means a score of 90 to 0 would have a lower rank than a score of 50 to 50, which is not what I want. (I'm trying to find a list of high scores, basically.)

I tried adding a function inside of the model, as such:

class ScoringModel(model.Model):
    red_score = models.SmallIntegerField()
    blue_score = models.SmallIntegerField()

    def max_score(self):
        return self.red_score if self.red_score > self.blue_score else self.blue_score

Which doesn't work, since the order_by function doesn't support function return values (ie ScoringModel.objects.order_by('-max_score') and ScoringModel.objects.order_by('-max_score()') fail to run).

Any help would be very appreciated

1 Answers1

0

I can suggest you to see this answer

https://stackoverflow.com/a/1875616/5518973

you can use mysql function GREATEST(field1, field2) instead of Sum and then order by that extra field.

Community
  • 1
  • 1
Aniket Pawar
  • 2,641
  • 19
  • 32