Perform the following steps to fetch book data from the library.book model:
- Add the jsonrpc_fetch_data.py file. You can place this file anywhere you want because the RPC program will work independently.
- Add the following code to the file:
# place authentication and get_json_payload methods (see first jsonrpc recipe)
if user_id:
# search for the books ids
search_domain = ['|', ['name', 'ilike', 'odoo'], ['name', 'ilike', 'sql']]
payload = get_json_payload("object", "execute_kw",
db_name, user_id, password,
'library.book', 'search', [search_domain], {'limit': 5})
res = requests.post(json_endpoint, data=payload, headers=headers).json()
print('Search Result:', res) # ids will be in result keys
# read data for books ids
payload = get_json_payload("object", "execute_kw",
db_name, user_id, password,
'library.book', 'read', [res['result'], ['name', 'date_release']])
res = requests.post(json_endpoint, data=payload, headers=headers).json()
print('Books data:', res)
else:
print("Failed: wrong credentials")
- Run the Python script from the Terminal with the following command:
python3 jsonrpc_fetch_data.py
The preceding program will give you the following output. The first RPC call will print the book's id and the second one will print the information for the book's id: