计算选择字段不起作用
Computed Selection field doesn´t work
我正在尝试创建一个应动态计算值的选择字段。声明为:
payment_mode = fields.Selection('_compute_selection', string="Payment mode", default="cash")
以及函数:
@api.model
def _compute_selection(self):
context = dict(self._context or {})
active_ids = context.get('active_ids')
active_model = context.get('active_model')
invoices = self.env[active_model].browse(active_ids)
type_payment = MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type]
if type_payment == "expense":
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal'),
('transfer', 'Bank transfer')]
else:
payment_mode = [('cash', 'Cash'),
('transfer', 'Bank transfer')]
else:
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal')]
else:
payment_mode = [('cash', 'Cash')]
return payment_mode
视图中的声明:
<field name="payment_mode" widget="radio" />
然而,在模块升级时我得到这个错误:
File "/home/openuser/erp10/odoo/models.py", line 1079, in _validate_fields
raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
ParseError: "Error while validating constraint
None
None" while parsing /home/openuser/erp10/addons/arn_payment/views/view.xml:3, near
知道我做错了什么吗?
我找不到关于此的任何文档,但我认为您只需要使用 lambda
作为 selection
属性,例如:
def _compute_selection(self):
active_model = self._name
type_payment = MAP_INVOICE_TYPE_PARTNER_TYPE[self[0].type]
if type_payment == "expense":
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal'),
('transfer', 'Bank transfer')]
else:
payment_mode = [('cash', 'Cash'),
('transfer', 'Bank transfer')]
else:
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal')]
else:
payment_mode = [('cash', 'Cash')]
return payment_mode
payment_mode = fields.Selection(selection=lambda self: self._compute_selection(), string="Payment Mode", default="cash")
注意:我已经将您的 _compute_selection
方法更改为不是 @api.model
并调整了您获取 active_model
和 type_payment
的方式。这些更改可能适合您,也可能不适合您。
主要内容是使用 selection=lambda self: self.method()
获取动态选择值。
我正在尝试创建一个应动态计算值的选择字段。声明为:
payment_mode = fields.Selection('_compute_selection', string="Payment mode", default="cash")
以及函数:
@api.model
def _compute_selection(self):
context = dict(self._context or {})
active_ids = context.get('active_ids')
active_model = context.get('active_model')
invoices = self.env[active_model].browse(active_ids)
type_payment = MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type]
if type_payment == "expense":
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal'),
('transfer', 'Bank transfer')]
else:
payment_mode = [('cash', 'Cash'),
('transfer', 'Bank transfer')]
else:
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal')]
else:
payment_mode = [('cash', 'Cash')]
return payment_mode
视图中的声明:
<field name="payment_mode" widget="radio" />
然而,在模块升级时我得到这个错误:
File "/home/openuser/erp10/odoo/models.py", line 1079, in _validate_fields
raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
ParseError: "Error while validating constraint
None
None" while parsing /home/openuser/erp10/addons/arn_payment/views/view.xml:3, near
知道我做错了什么吗?
我找不到关于此的任何文档,但我认为您只需要使用 lambda
作为 selection
属性,例如:
def _compute_selection(self):
active_model = self._name
type_payment = MAP_INVOICE_TYPE_PARTNER_TYPE[self[0].type]
if type_payment == "expense":
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal'),
('transfer', 'Bank transfer')]
else:
payment_mode = [('cash', 'Cash'),
('transfer', 'Bank transfer')]
else:
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal')]
else:
payment_mode = [('cash', 'Cash')]
return payment_mode
payment_mode = fields.Selection(selection=lambda self: self._compute_selection(), string="Payment Mode", default="cash")
注意:我已经将您的 _compute_selection
方法更改为不是 @api.model
并调整了您获取 active_model
和 type_payment
的方式。这些更改可能适合您,也可能不适合您。
主要内容是使用 selection=lambda self: self.method()
获取动态选择值。