I am facing an issue while trying to define an action server in Odoo14. The objective is to call a Python method that returns a window action because I need to access fields in the current model and pass them to the context. Unfortunately, directly using a window action with a context that calls fields doesn't seem to be working as expected. Below is the code snippet I am using:
XML Code:
<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 Method:
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
However, I'm encountering the following error when I click on action button:
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()'
I would appreciate any assistance in identifying and correcting the issue in the code, or any alternative suggestions for defining the action server correctly.
The issue was with how the action values were returned into the Python method. Here is the updated code:
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
}