在域函数中引用另一个字段 (Odoo12)

Referencing another field in a domain function (Odoo12)

我想根据在 GL 前缀中输入的值来限制帐户查找(域)(我实际上会使用一些通配符和一些我稍后会很乐意添加的其他逻辑),问题是我' m 获取 self.x_poLineGLprefix 返回的逻辑 True 或 False 值,而不是字段中的值。如何获取 x_poLineGLprefix 的实际数据值?

class QuickPOLine(models.Model): 
_name = 'purchase.order.line' 
_inherit = 'purchase.order.line' 

x_poLineGLprefix = fields.Char(string='GL Prefix') 
x_poLineGLaccount = fields.Many2one( 
'account.account', string="Line Item Expense Account", 
domain=lambda self: [('code', '=', self.x_poLineGLprefix)])

您可以基于任何字段添加动态域来实现这一点。在@api.onchange() 函数中,您可以 return 域用于 many2one。要添加动态域,您可以参考此 link. If you are using both many2one fields refer this link.

试试这个

@api.onchange('x_poLineGLprefix')
def onchange_x_poLineGLprefix(self):

    if self.x_poLineGLprefix:
         return {'domain': {
            'x_poLineGLaccount': [('code', '=', self.x_poLineGLprefix)]
        }}