Enable Dark Mode!
how-to-load-models-and-fields-to-pos-in-odoo-17.jpg
By: Muhsina V

How to Load Models & Fields to POS in Odoo 17

Technical Odoo 17

Odoo 17 is the latest version of the popular open-source ERP software. It comes with a number of new features and improvements, including a more robust and user-friendly POS module. One of the key features of the new Odoo 17 POS module is the ability to load custom models and fields. This allows you to extend the POS's functionality to meet your business's specific needs.

Loading models

To load a custom model into the POS, you need to inherit the pos.session model and overwrite the _pos_ui_models_to_load() method. This method returns a list of all the models that should be loaded into the POS. To add a new model to the list, simply append its name to the list.
For example, the following code shows how to load a custom model called custom.model into the POS:
from odoo import models 
PosSession(models.Model):
    “””Extends the `pos.session` model.”””
    _inherit = 'pos.session'
def _pos_ui_models_to_load(self):
    res = super()._pos_ui_models_to_load()
    res.append('custom.model')
    return res

Loading fields

To load a custom field into the POS, you need to define a new function called _loader_params_custom_model(). This function should return a dictionary with a key called search_params. The value of search_params should be a dictionary with two keys. They are domains and fields. The domain should be a list with a tuple specifying filters, and fields should be a list of field names that you want to load into the POS.
For example, the following code shows how to load a custom field called custom_field from the custom.model model into the POS:
Python
from odoo import models
PosSession(models.Model):
    “””Extends the `pos.session` model.”””
    _inherit = 'pos.session'
    def _pos_ui_models_to_load(self):
        res = super()._pos_ui_models_to_load()
        res.append('custom.model')
        return res
    def _loader_params_custom_model(self):
        return {
            'search_params': {
                'domain': [('state', '=', 'draft')],
                'fields': ['name', 'partner_id', 'state',     'custom_field'],
            },
        }
Create a new function called _get_pos_ui_custom_model that accepts a params parameter. This function should use the search_read method of the env object to retrieve records from the custom.model model, using the search parameters provided in the params dictionary.
def _get_pos_ui_custom_model(self, params):
return       self.env['custom.model'].search_read(**params['search_params'])
To load the custom model data into the POS, patch the PosStore, and override the _processData function, we need to import the necessary components. Import the patch function and PosStore from their respective folders. The patch function is a powerful tool for applying changes to existing classes or methods, while PosStore serves as the central repository for the Point of Sale application's state.
/** @odoo-module */
import { patch } from "@web/core/utils/patch";
import { PosStore } from "@point_of_sale/app/store/pos_store";
patch(PosStore.prototype, {
     async _processData(loadedData) {
        await super._processData(...arguments);
        this.custom_model = loadedData["custom.model"];
        }
});
This code snippet ensures that we import the necessary elements, apply the patch to modify the PosStore class, and override the _processData function. The use of super._processData guarantees the preservation of the function's original behavior. Additionally, by assigning the data related to the custom model to the custom_model property, the POS application can access and use data related to the custom model.

Loading fields to existing models in POS

To add a custom field into an existing model, override the _loader_params_existing_model function (replace "existing_model" with the target model). Within this override, invoke the parent function and augment the custom field to the "search_params" within the "fields" list.
    def _loader_params_existing_model(self):
        result = super()._loader_params_existing_model()
        result['search_params']['fields'].append(custom_field)
        return result
This is how we can load the models and their fields, and loading custom fields into an existing model is accomplished.
To read more about load models & fields in the Odoo 16 POS, refer to our blog How to Load Models & Fields in the Odoo 16 POS


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message