Python 定义了条件的和

Python 定义了条件的和,python,odoo,Python,Odoo,我执行的方法只有在“现金”账户中时才需要添加,但当以下面的方式执行时,它对我不起作用。任何人都知道如何添加条件并添加它 class Account (models.Model): _name = 'project_rc.account' name = fields.Char (string = "Name") total_account_must = fields.Float (string = "Total account must", compute = "_tot

我执行的方法只有在“现金”账户中时才需要添加,但当以下面的方式执行时,它对我不起作用。任何人都知道如何添加条件并添加它

class Account (models.Model):
    _name = 'project_rc.account'

    name = fields.Char (string = "Name")
    total_account_must = fields.Float (string = "Total account must", compute = "_total_account_must")
    total_account_credit = fields.Float (string = "Total account credit", compute = "_total_account_credit")
    detail_document_ids = fields.One2many (comodel_name = 'project_rc.detail_document', inverse_name = 'account_id', string = 'Document detail',required = True)


    @ api.one
    @ api.depends (detail_document_ids)
    def _total_account_must(self):
        sum = 0
        if self.title == "Cash":
            for detail_document in self.detail_document_ids:
                 sum + = detail_document.total_must
                 self.total_account_must = sum

class Document_Detail (models.Model):
    _name = 'project_rc.detail_document'

   total_must = fields.Float (string = "Input value")
   total_credit = fields.Float (string = "Output value")
   account_id = fields.Many2one (comodel_name = 'project_rc.account', string = 'Account')

但是在这个名为document\u document\u ids的字段中,我有点困惑,您在Compute中使用了detail\u document\u ids

尽力给予

@ api.one
@ api.depends (detail_document_ids.total_must)
def _total_account_must(self):
    self.total_account_must = sum(line.total_must for line in self.detail_document_ids) if self.title == "Cash" else 0

给依赖者一个字符串

@api.depends('detail_document_ids')

还有“总账户”信用功能吗?

self.title是“现金”账户吗?你能解释一下到底是什么问题吗?一个人可以创建很多账户,要么是“现金”账户,要么是“银行”,每个账户都有变动,所以我想添加每个账户的所有变动,所以有两个表是“账户”和“明细变动”。我留下了一张照片,这样你可以更好地理解,如果在文字上没有做正确。不工作是什么意思,方法不被调用?计算值有误吗?有错误吗?当我说它不工作时,它不会运行,并且出现错误,因为它是我尝试添加的方法的一般错误,因此未定义错误。代码不工作。作为document_document_id提到的字段是detail_document_id。这只是一个翻译错误,但上面已经更正了。现在我提到的代码不起作用,这导致了我的错误。还有其他方法吗?我编辑:当执行程序时,它会向我打开它,但当我进入与“帐户”对应的窗口时,我会得到以下错误。AttributeError:'project\u rc.detail\u document'对象没有属性'detail\u document\u ID'您好,如果两个函数都有,但没有执行信用,因为首先我想看看其中一个是否工作,然后以与另一个相同的方式操作。尝试只放置详细文档ID,但它不起作用。I edit:当执行程序时,它会向我打开它,但当我进入与“accounts”对应的窗口时,我得到以下错误。AttributeError:“project_rc.detail_document”对象没有属性“detail_document_id”AttributeError:“project_rc.detail_document”对象没有属性“detail_document_id”@FranciscogongzálezMejí,因为您收到的错误将表明您将函数放入了错误的类中(它正在尝试访问项目\ rc.detail\文档模型上的详细\文档\ ID,而不是项目\ rc.account模型)我假设total_account_must和total_account_credit字段都将依赖于detail_document_id,因此同时计算两者是有意义的。此外,如果您有一个depends on def_total_account_must定义,您可能希望total_account_must字段定义中的store=True。我认为有是一个错误,因为该方法被放置在“account”类中,该类是“cash”和“bank”帐户的注册位置,因此我希望在该类中查看每个帐户的总和。我有问题初始描述中的代码。