1

I am developing a script to create a record in a model of an Odoo. I need to run this model's methods on specific records. In my case the method which I need to run on a specific record doesn't have any parameter (just has self). I want to know how can I run the method on a specific record of the model through xmlrpc call from client to Odoo server. Below is the way I tried to call the method and pass the id of a specific record regarding this question.

xmlrpc_object.execute('test_db', user, 'admin', 'test.test', 'action_check_constraint', [record_id])

action_check_constraint checks some constraints on each record of the model and if all the constraints passed, changes the state of the record or raise validation errors. But the above method call with xmlrpc raise below error:

xmlrpc.client.Fault: <Fault cannot marshal None unless allow_none is enabled: 'Traceback (most recent call last):\n  File "/home/ibrahim/workspace/odoo13/odoo/odoo/addons/base/controllers/rpc.py", line 60, in xmlrpc_1\n    response = self._xmlrpc(service)\n  File "/home/ibrahim/workspace/odoo13/odoo/odoo/addons/base/controllers/rpc.py", line 50, in _xmlrpc\n    return dumps((result,), methodresponse=1, allow_none=False)\n  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 968, in dumps\n    data = m.dumps(params)\n  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 501, in dumps\n    dump(v, write)\n  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 523, in __dump\n    f(self, value, write)\n  File "/usr/local/lib/python3.8/xmlrpc/client.py", line 527, in dump_nil\n    raise TypeError("cannot marshal None unless allow_none is enabled")\nTypeError: cannot marshal None unless allow_none is enabled\n'>
> /home/ibrahim/workspace/scripts/automate/automate_record_creation.py(328)create_record()

Can anyone help with the correct and best way of calling a model's method (with no parameter except self) on a specific record through xmlrpc client to Odoo server?

Ibrahim Rahimi
  • 519
  • 8
  • 31

3 Answers3

1

That error is raised, because the xmlrpc library is not allowing None as return value as default. But you should change that behaviour by just allowing it.

Following line is from Odoo's external API documentation, extended to allow None as return value:

models = xmlrpc.client.ServerProxy(
    '{}/xmlrpc/2/object'.format(url), allow_none=True)

For more information about xmlrpc ServerProxy look into the python documentation

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • You're right, but the problem is I added `allow_none=True` but still have the error when my method in this case `action_check_constraint` doesn't explicitly returns anything. `xmlrpc_objects = client.ServerProxy('http://' + 'localhost:8000/xmlrpc/object',allow_none=True)` . In such case what should I do? – Ibrahim Rahimi Nov 30 '20 at 12:52
  • Returning nothing in python is returning `None` ([look into](https://stackoverflow.com/questions/15300550/return-return-none-and-no-return-at-all)) – CZoellner Nov 30 '20 at 13:11
1

You can get the error if action_check_constraint does not return anything (by default None).

Try to run the server with the log-level option set to debug_rpc_answer to get more details.

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • You're right about the return by method. Now I added `allow_none=True` and due to some concerns I am not able to modify the Odoo method to add explicit return. Also, the error still exist. – Ibrahim Rahimi Nov 30 '20 at 12:55
  • Odoo disables explicitly the ``None`` type in the [base rpc](https://github.com/odoo/odoo/blob/13.0/odoo/addons/base/controllers/rpc.py) controller. There is an Odoo issue in GitHub related to a [method which returns None over XML-RPC](https://github.com/odoo/odoo/issues/29768). – Kenly Nov 30 '20 at 13:27
  • I read all those answers and got your point. But my problem is that using allow_none=True is not solving the problem. Also, because I am not allowed to bring changes in source code of that project I can't modify those methods to return anything than None. So, what else I can do? – Ibrahim Rahimi Dec 01 '20 at 05:46
  • 1
    Enabling the option only on the client-side will not work as you can read in the [xmo-odoo](https://github.com/odoo/odoo/issues/29768#issuecomment-571523108) comment: ``as it's an optional feature it has to be enabled on both client and server, otherwise whichever side doesn't support it will blow up. And of course, there's the issue that if this is allowed on the server it will break clients not expecting it, so it's a bit of a tricky pickle``. – Kenly Dec 01 '20 at 15:18
0

After lost of search and try first I used this fix to solve the error but I think this fix is not a best practice. So, I found OdooRPC which does the same job but it handled the above case and there's no such error for model methods which return None. Using OdooRPC solved my problem and I done what I needed to do with xmlrpc in Odoo.

Ibrahim Rahimi
  • 519
  • 8
  • 31