How Does RPC & ORM Calls Works in Odoo 16

How RPC Works in Odoo Framework:

*Odoo is an open-source ERP (Enterprise Resource Planning) framework that provides a vast range of business application functionalities. It follows a client-server architecture, where the client interacts with the server to perform various operations like reading data, creating records, updating records, and more.

1. Odoo ORM (Object-Relational Mapping)

*The heart of Odoo's data manipulation and communication with the database is the Odoo ORM, which stands for Object-Relational Mapping. The ORM acts as an abstraction layer between the Python code and the underlying database, allowing developers to interact with data in a more Pythonic way. It helps manage the data models and map them to the database tables.

this.orm = useService('orm')
const resPartner = await 
this.orm.call('res.partner','search_read',[[]])

  

This ORM call triggers the call function in the orm_service.js file
 
 
call(model, method, args = [], kwargs = {}) {
    validateModel(model);
    const url = `/web/dataset/call_kw/${model}/${method}`;
    const fullContext = Object.assign({}, this.user.context, kwargs.context || {});
    const fullKwargs = Object.assign({}, kwargs, { context: fullContext });
    const params = {
        model,
        method,
        args,
        kwargs: fullKwargs,
    };
    return this.rpc(url, params, { silent: this._silent });
}

  

The rpc function in the rpc_service.js ultimately starts the jsonrpc function 
 
 
export const rpcService = {
    async: true,
    start(env) {
        let rpcId = 0;
        return function rpc(route, params = {}, settings) {
            return jsonrpc(env, rpcId++, route, params, settings);
        };
    },
};

  

The Data is returned to the source of the call.

2.XML-RPC and JSON-RPC

  *Odoo provides two main RPC protocols for client-server communication: XML-RPC and JSON-RPC. XML-RPC uses XML to encode the request and response data, while JSON-RPC uses JSON (JavaScript Object Notation). Both protocols are lightweight and platform-independent, making them suitable for communication between different systems.
Refer this  XML-RPC

3.API Endpoints

  *Odoo exposes a set of API endpoints that can be accessed by clients to perform various operations. These endpoints represent different Odoo services, such as objects, methods, workflows, and more. Clients can make RPC requests to these API endpoints to interact with the Odoo server.
This is one example of the api endpoint.
@http.route(['/web/dataset/call_kw', '/web/dataset/call_kw/<path:path>'], type='json', auth="user")
    def call_kw(self, model, method, args, kwargs, path=None):
        return self._call_kw(model, method, args, kwargs)

  

4.RPC Methods

*Odoo's API provides several RPC methods that clients can use to perform CRUD (Create, Read, Update, Delete) operations on Odoo models. Some commonly used RPC methods include:
search_read: To search and read records from the Odoo database.
create: To create new records in the Odoo database.
write: To update existing records in the Odoo database.
unlink: To delete records from the Odoo database.

5.Security

*Odoo ensures security during RPC calls by enforcing access controls based on user roles and permissions. It prevents unauthorized access to sensitive data and operations.

6.Session Management

*When a client initiates an RPC call, Odoo maintains a session for that client, keeping track of their context and rights during subsequent requests. This session management helps in maintaining the state of the client-server interaction.
In conclusion, RPC in the Odoo framework enables seamless communication between the client and server components. The use of XML-RPC or JSON-RPC allows clients to make requests to Odoo's API endpoints, perform various operations on the database, and retrieve the desired results. This RPC mechanism, combined with Odoo's powerful ORM and security features, makes it a robust and efficient framework for building enterprise-grade applications.
RPC
*RPC, short for Remote Procedure Call, is a communication protocol used in distributed systems to enable different software components or processes to communicate with each other across a network. It allows programs running on different devices or machines to invoke functions or procedures on remote systems as if they were local, making it easier to build complex and interconnected applications.
*In simple terms, RPC allows a client application to request services or functions from a server application located on a different machine over a network. The client and server interact as if they were on the same system, abstracting the complexity of network communication and providing a seamless way to distribute tasks and processing across multiple nodes.
There are several different ways you call an rpc call ;
const rpc = require('web.rpc')
await rpc.query({
        model: 'res.partner',
        method: 'search_read',
        args : []
      }).then(data => {
         // The data will have the called objects in an array
      })

  

The flow of this rpc call is as follows:
*The query function in web/core/rpc.js file get triggered and the buildQuery function builds the query which has the url for the api endpoint
query: function (params, options) {
    var query = rpc.buildQuery(params);
    return ajax.rpc(query.route, query.params, options);
    },

  

*Then rpc function in the ajax.js file is triggered and jsonRpc function is returned
*The url in jsonRpc would look something like this ‘/web/dataset/call_kw/res.partner/search_read
*Then jsonRpc ultimately calls ajax function in the jquery.js file then the remote procedure call triggers the Controller function call_kw
@http.route(['/web/dataset/call_kw', '/web/dataset/call_kw/<path:path>'], type='json', auth="user")
    def call_kw(self, model, method, args, kwargs, path=None):
        return self._call_kw(model, method, args, kwargs)

  

*And after that call_kw function in the api.py file is triggered and the function 'search_read' in the model 'res.partner' is executed.
 
posted @ 2023-10-01 13:29  CrossPython  阅读(46)  评论(0编辑  收藏  举报