域字段 many2one 基于另一个 many2one 字段 Odoo 11

问题描述

大家好我是odoo新手我创建了一个模块来修改会计模块。我想修改供应商帐单和客户发票,并且 AI 添加了一个名为“预算”的新列以获取所有预算,当我选择预算以仅显示与其相关的分析帐户时,这是我想要的多对一字段。

class custom_accounting_edit(models.Model):
    _inherit = 'crossovered.budget.lines'

    analytic_account_id = fields.Many2one('account.analytic.account','Budget line')


class BillsEdit(models.Model):
    _inherit = 'account.invoice.line'

    budget_id = fields.Many2one('crossovered.budget',string='Budget')
    account_analytic_id = fields.Many2one('account.analytic.account',string='Budget line')

    @api.onchange('budget_id')
    def onchange_analytic_account_id(self):
        for rec in self:
            return {'domain': {'analytic_account_id': [('analytic_account_id','=',rec.budget_id.id)]}}

enter image description here

解决方法

在包含表单中存在的值的伪记录上调用该方法,因此您无需循环遍历 self

您需要使用域中的 id 字段来使用预算行分析帐户的值过滤帐户(它将返回 id 字段值)。

您可以使用 mapped 返回所有预算分析帐户的联合,并删除重复项。

@api.onchange('budget_id')
def onchange_analytic_account_id(self):
    return {'domain': {'account_analytic_id': [
        ('id','in',self.mapped('budget_id.crossovered_budget_line.analytic_account_id.id')
         )]
    }
    }
,

试试这个

@api.onchange('budget_id')
def onchange_analytic_account_id(self):
    account_ids = [line.analytic_account_id.id for line in self.budget_id.crossovered_budget_line]
    return {'domain': {'analytic_account_id': [('id',account_ids)]}}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...