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

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

问题描述

我知道这个简短的答案,因为我试过了。有没有办法完成这个(即使只是因为一个黑客)?

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

class Meta:
order = ['created']

@property
def repair_cost(self):
#cost是LineItem的一个@property(模型.Model)
return self.lineitem_set.aggregate(models.Sum('cost'))


解决方案

不。通过内置管理器进行的任何操作都必须是一个真正的领域,因为它们只触及数据库。为了与一个财产一起工作,他们必须将表格中的每个记录都转换成一个模型,然后用Python过滤它们。


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'))

解决方案

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天全站免登陆