Django - 您可以使用属性作为聚合函数中的字段吗? [英] Django - Can you use property as the field in an aggregation function?

查看:27
本文介绍了Django - 您可以使用属性作为聚合函数中的字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道简短的答案,因为我试过了.有什么办法可以做到这一点(即使只是因为黑客攻击)?

I know the short answer because I tried it. Is there any way to accomplish this though (even if only on account of a hack)?

class Ticket(models.Model):
    account = modelfields.AccountField()
    uuid = models.CharField(max_length=36, unique=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['created']

    @property
    def repair_cost(self):
        # cost is a @property of LineItem(models.Model)
        return self.lineitem_set.aggregate(models.Sum('cost'))

推荐答案

没有.任何通过内置管理器的东西都必须是一个真实的领域,因为它们只接触数据库.为了使用属性,他们必须将表中的每条记录转换为模型,然后在 Python 中过滤它们.

No. Anything that goes through a built-in manager has to be a real field, since they only touch the database. In order to work with a property they'd have to turn every record in the table into a model, then filter through them in Python.

这篇关于Django - 您可以使用属性作为聚合函数中的字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆