English 中文(简体)
我怎样才能用两个字段的总和来排序对接查询?
原标题:How can I sort a django queryset by the total of two fields?

我想加注一个对象列表( 每个对象都有一套 tags 和一个整数值 points ), 以便按照标记加点的总计数进行分类。

然而,我无法用两栏之和来说明物体。

这些是我正在使用的模型,而lederboard () 函数是我难以工作的模型。

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 )

<强度>编辑2: 使用额外

好吧,所以我得到了额外的工作, 通过手动计数标签数量 并添加到点数。

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 ,)
    )
最佳回答

额外使用

players.objects.extra(
    select={ fieldsum : tags__count + points },
    order_by=( fieldsum ,)
)
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签