How it works...

In the Logging in/connecting Odoo with JSON-RPC recipe, we saw that you can validate username and password. If the login details are correct, the RPC call will return user_id. You can then use this user_id to fetch the model's data. Like XML-RPC, we need to use the search and read combination to fetch the data from the model. To fetch the data, we use object as a service and execute_kw as the method. execute_kw is the same method that we used in XML-RPC  for data, so it accepts the same argument, as follows:

  • Database name
  • User ID (we get this from the authenticate method)
  • Password
  • Model name, for example, res.partner, library.book
  • Method name, for example, search, read, create
  • An array of positional arguments (args)
  • A dictionary for keyword arguments (optional) (kwargs)

In our example, we called the search method first. The execute_kw method usually takes mandatory arguments as positional arguments, and optional arguments as keyword arguments. In the search method, domain is a mandatory argument so we passed it in the list and passed the optional argument limit as the keyword argument (dictionary). You will get a response in the JSON format, and in this recipe the response of the search() method RPC will have the book's IDs in the result key.

In step 2, we made an RPC call with the read method. To read the book's information, we passed two positional arguments—the list of book IDs and the list of fields to fetch. This RPC call will return the book information in JSON format and you can access it in the result key.

Instead of execute_kw, you can use execute as the method. This does not support keyword arguments, so you need to pass all of the intermediate arguments if you want to pass some optional arguments.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset