我在尝试在 Odoo14 中定义操作服务器时遇到问题。目标是调用返回窗口操作的 Python 方法,因为我需要访问当前模型中的字段并将它们传递到上下文。不幸的是,直接使用带有调用字段的上下文的窗口操作似乎无法按预期工作。以下是我正在使用的代码片段:
XML 代码:
<field name="name">Créer un paiement</field>
<field name="model_id" ref="account.model_account_bank_statement_line"/>
<field name="binding_model_id" ref="account.model_account_bank_statement_line"/>
<field name="state">code</field>
<field name="binding_view_types">list</field>
<field name="code">action = model.action_payment_bank_statement_line()</field>
Python方法:
def action_payment_bank_statement_line(self):
action_ref = 'account.view_account_payment_form'
print(action_ref)
action = self.env['ir.actions.act_window']._for_xml_id(action_ref)
action['context'] = {
'default_payment_type': 'outbound',
'default_partner_type': 'customer',
'default_ref': self.payment_ref,
'default_amount': self.amount,
'default_date': datetime.strptime(self.date, '%Y-%m-%d')
}
action['view_mode'] = 'form'
action['target'] = 'new'
return action
但是,当我单击操作按钮时遇到以下错误:
result = request.dispatch()
File "/opt/odoo14/odoo/odoo/http.py", line 696, in dispatch
result = self._call_function(**self.params)
File "/opt/odoo14/odoo/odoo/http.py", line 370, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/odoo14/odoo/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/odoo14/odoo/odoo/http.py", line 358, in checked_call
result = self.endpoint(*a, **kw)
File "/opt/odoo14/odoo/odoo/http.py", line 919, in __call__
return self.method(*args, **kw)
File "/opt/odoo14/odoo/odoo/http.py", line 544, in response_wrap
response = f(*args, **kw)
File "/opt/odoo14/odoo/addons/web/controllers/main.py", line 1728, in run
result = action.run()
File "/opt/odoo14/odoo/odoo/addons/base/models/ir_actions.py", line 632, in run
res = runner(run_self, eval_context=eval_context)
File "/opt/odoo14/odoo/addons/website/models/ir_actions.py", line 61, in _run_action_code_multi
res = super(ServerAction, self)._run_action_code_multi(eval_context)
File "/opt/odoo14/odoo/odoo/addons/base/models/ir_actions.py", line 501, in _run_action_code_multi
safe_eval(self.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/opt/odoo14/odoo/odoo/tools/safe_eval.py", line 347, in safe_eval
raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr))
Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/odoo14/odoo/odoo/http.py", line 652, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/odoo14/odoo/odoo/http.py", line 317, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: <class 'AssertionError'>: "" while evaluating
'action = record.action_payment_bank_statement_line()'
如果您能在识别和纠正代码中的问题方面获得任何帮助,或者正确定义操作服务器的任何替代建议,我将不胜感激。
问题在于如何将操作值返回到 Python 方法中。这是更新后的代码:
def action_payment_bank_statement_line(self):
view_id = self.env.ref('account.view_account_payment_form').id
ctx = {
'default_payment_type': 'inbound',
'default_partner_type': 'customer',
'default_ref': self.payment_ref,
'default_amount': self.amount,
'default_date': datetime.strptime(str(self.date), '%Y-%m-%d').date()
}
return {'type': 'ir.actions.act_window',
'res_model': 'account.payment',
'target': 'new',
'view_mode': 'form',
'views': [[view_id, 'form']],
'context': ctx
}