Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a django queryset by the total of two fields?

I want to annotate a list of objects (which each have a set of tags and an integer value points) so that they can be sorted by the total of count of the tags plus the points.

However, I cannot find any way to annotate the objects with the sum of two columns. Any idea as to how I should do this?

These are the models I am using, with the leaderboard() function being the one that I am having difficulty making work.

class Game (models.Model):
    users = models.ManyToManyField(User, through='Player', related_name='games')

    def leaderboard (self):
        """ Return a list of players ranked by score, where score is the total of the count of the players tags and their points. """
        leaderboard = self.player_set.annotate(
            tagcount=models.Count('tags')
        ).extra(
            select={'score': 'tagcount + points'},
            order_by=('score',)
        )
        return leaderboard

class Player (models.Model):
    game = models.ForeignKey(Game)
    user = models.ForeignKey(User)
    points = models.SmallIntegerField(default=0, help_text="Points that have been awarded to the player")

class Tag (models.Model):
    game = models.ForeignKey(Game, related_name='tags')
    player = ForeignKey(Player, related_name='tags')

Edit 2: Solution using extra

Okay, so I got extra to work, by manually counting the number of tags and adding that to the points.

def leaderboard (self):
    """ Return a list of players ranked by score, where score is the total of the count of the players tags and their points. """
    return self.players.extra(
        select={'score': '(select count(*) from kaos_tag where kaos_tag.tagger_id=kaos_player.id) + points'},
        order_by=('-score',)
    )
like image 642
borntyping Avatar asked Jan 22 '26 13:01

borntyping


1 Answers

use extra

  • Django | QuerySet API reference | Django documentation -> https://docs.djangoproject.com/en/1.3/ref/models/querysets/#extra
players.objects.extra(
    select={'fieldsum':'tags__count + points'},
    order_by=('fieldsum',)
)
like image 88
Mohammad Efazati Avatar answered Jan 25 '26 21:01

Mohammad Efazati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!