嘿,我想在 Django 中根据计算值对对象进行排序...我该怎么做?
这是一个基于堆栈溢出的用户配置文件模型示例,它解释了我的困境:
class Profile(models.Model):
user = models.ForeignKey(User)
def get_reputation():
...
return reputation
reputation = property(get_reputation)
所以,假设我想按声誉对用户进行排序。我怎么做?我知道你不能只这样做:
Profile.objects.order_by("-reputation")
感谢大家的帮助:)
最佳答案
由于您的计算代码仅存在于 Python 中,因此您也必须在 Python 中执行排序:
sorted (Profile.objects.all (), key = lambda p: p.reputation)
关于python - 如何按django中的计算值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/930865/