In this blog post, we will explore the process of loading models and fields in POS Odoo 16. POS, or Point of Sale, is a crucial component of any business that serves as the interface between the customer and the company's sales and inventory management systems. With the release of Odoo 16, a new version of the popular open-source business software, there have been several updates to the POS module that make it more user-friendly and efficient. This blog will cover the basics of loading models and fields in POS, including how to create custom models and fields and how to access them in the POS interface. By the end of this blog, you will have a solid understanding of how to manage models and fields in POS Odoo 16 and how to use them to streamline your business operations.
How to load models in POS Odoo16?
First, inherit the 'pos.session' model in your python file. Then, use the 'super' function to extend the '_pos_ui_models_to_load' function and add your new models to the result list. Your updated function should look like this:
def _pos_ui_models_to_load(self):
result = super()._pos_ui_models_to_load()
result.append('your.model')
return result.
Then, define a new function called '_loader_params_your_model' that returns a dictionary with a key called 'search_params' (replace your model with the name of the model you want). The value of 'search_params' should be a dictionary with two keys: 'domain' and 'fields.' The value of 'domain' should be a list with a tuple specifying filters. The value of 'fields' should be a list of field names that you want to load into POS. Your function should look like this:
def _loader_params_your_model(self):
return {
'search_params': {
'domain': [('state', '=', 'draft')],
'fields': ['name', 'partner_id', 'state'],
},
}
Then, define a new function called '_get_pos_ui_your_model' that takes a parameter called 'params' (replace your model with the name of the model you want). The function should use the 'search_read' method of the 'env' object to search and read records from the 'your.model' model, using the search parameters passed in the 'params' dictionary. Your function should look like this:
def _get_pos_ui_your_model(self, params):
return self.env['your.model'].search_read(**params['search_params'])
After this, we have to create a new javascript file and define a new class that extends the 'PosGlobalState' model. Then, add a new method called '_processData' that takes a parameter called 'loadedData'. In this method, use the 'super' function to call the original '_processData' method and pass in the necessary arguments. Then, add a new property to 'this' with some name and assign it the value of the 'your.model' key in the 'loadedData' dictionary. Finally, use the 'extend' method of the 'Registries.Model' object to extend the 'PosGlobalState' model with the 'NewPosGlobalState' class. Your code should look like this:
import { PosGlobalState} from 'point_of_sale.models';
import Registries from 'point_of_sale.Registries';
const NewPosGlobalState = (PosGlobalState) => class NewPosGlobalState extends PosGlobalState {
async _processData(loadedData) {
await super._processData(...arguments);
this.your_model = loadedData['your.model'];
}
}
Registries.Model.extend(PosGlobalState, NewPosGlobalState);
How to load fields to existing in POS Odoo16?
First, inherit the 'pos.session' model in your python file. Define a new function called '_loader_params_your_model' that returns a modified version of the result from the parent function (replace your model with name of the model you want). To do this, first, call the parent function using the 'super' function and store the result in a variable. Then, use the 'extend' method of the 'fields' list to add new fields to the list. Finally, return the modified dictionary. Your function should look like this
def _loader_params_your_model(self):
result = super()._loader_params_your_model()
result['search_params']['fields'].extend(['field_to_add'])
return result