How it works...

In order to access the book data, first you have to carry out authentication. At the beginning of the program, we did the authentication in the same way as we did in the Logging in/connecting Odoo with XML-RPC recipe earlier. If you provided valid credentials, the authentication method will return the id of the user's record. We will use this user ID to fetch the book data.

The /xmlrpc/2/object endpoint is used for database operation. In our recipe, we used the object endpoint to fetch the book data. In contrast to the /xmlrpc/2/common endpoint, this endpoint does not work without credentials. With this endpoint, you can access the public method of any model through the execute_kw() method. execute_kw takes the following arguments:

  • 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
  • A dictionary for keyword arguments (optional)

In our example, we want to fetch the book's information. This can be done through a combination of search() and read(). Book information is stored in the library.book model so in execute_kw(), we use library.book as the model name and search as the method name. This will call the ORM's search method and returns record IDs. The only difference here is that ORM's search method returns a recordset while this search method returns a list of IDs.

In execute_kw(), you can pass arguments and keyword arguments for the provided method. The search() method accepts a domain as a positional argument so we passed a domain to filter books. The search method has other optional keyword arguments, such as limit, offset, count, and order, from which we have used the limit parameter to fetch only five records. This will return the list of book IDs whose names contain the odoo or SQL strings. Still, we need to fetch book data from the database. We will use the read method to do this. The read method accepts a list of IDs and fields to complete the task. At the end of step 3, we used the list of book IDs that we received from the search method then used the book IDs to fetch the name and release_date of the books. This will return the list of the dictionary with the book's information.

Note that the arguments and keyword arguments passed in execute_kw() are based on the passed method. You can use any public ORM method via execute_kw(). You just need to give the method name, the valid arguments, and the keyword arguments. These arguments are going to be passed in the method in the ORM.
..................Content has been hidden....................

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