Enable Dark Mode!
how-database-population-works-in-odoo-18.jpg
By: Amaya Aravind EV

How Database Population Works in Odoo 18

Technical

For any software being developed, the key to success and a perfect outcome depends on the perfect testing and review process carried out in every aspect. In Odoo 18, for that performance analysis, we use the method called database population, which automatically generates a demo or sample record creation in the database with which we test the code. By this method, the developer can oversee the potential issues along with the performance testing and the system benchmarking for the code developed. For this kind of efficiency evaluation process, the database population is carried out in Odoo with the aid of the Command Line Interface (CLI) tool. Let’s discuss some of the main methods we use for this database population in Odoo.

_populate_sizes():

This method usually returns a dictionary that maps the sizes ('small', 'medium', 'large'). Default sizes are small: 10, medium: 100, large: 1000

_populate(size):

This method creates records to populate the corresponding model. The parameter size is a string that can take the values small, medium, or large.

_populate_dependencies():

This method returns dependent models of the current populating model. It defines the order in which dependencies (related models) should be populated before the current model.

_populate_factories():

A factory is a dictionary of field values. A factory for various fields of the related model will be filled by this function. A list of pairs (field_name, factory) with `factory` being a generator function will be returned.

To demonstrate with an example and for the purpose of database population, we need to use at least the population methods _populate(size) and _populate_factories()

For example, take the model product.category. Let’s define the populating methods for the model as

from odoo import api, fields, models, _
from odoo.tools import populate
import logging
import collections
_logger = logging.getLogger(__name__)
class ProductCategory(models.Model):
    _inherit = "product.category"
    _populate_sizes = {"small": 50, "medium": 500, "large": 5000}
    def _populate_factories(self):
        return [("name", populate.constant('PC_{counter}'))]
    def _populate(self, size):
        categories = super()._populate(size)
        # Set parent/child relation
        self._populate_set_parents(categories, size)
        return categories
    def _populate_set_parents(self, categories, size):
        _logger.info('Set parent/child relation of product categories')
        parent_ids = []
        rand = populate.Random('product.category+parent_generator')
        for category in categories:
            if rand.random() < 0.25:
                parent_ids.append(category.id)
        categories -= self.browse(parent_ids)  # Avoid recursion in parent-child relations.
        parent_childs = collections.defaultdict(lambda: self.env['product.category'])
        for category in categories:
            if rand.random() < 0.25:  # 1/4 of remaining categories have a parent.
                parent_childs[rand.choice(parent_ids)] |= category
        for count, (parent, children) in enumerate(parent_childs.items()):
            if (count + 1) % 1000 == 0:
                _logger.info('Setting parent: %s/%s', count + 1, len(parent_childs))
            children.write({'parent_id': parent})

Here,  the categories are created, and parents are added so that some of them can behave as subcategories. The message is displayed in the log window to show that the data population is taking place. For populating the data, first we need to go to the directory of odoo-bin and type the command on the server as

doo-bin populate --models model_name -d database_name

While running the code, we can see the logger messages in the log window as

How Database Population Works in Odoo 18-cybrosys

And as the code runs, data will get populated with the code, and they can be seen as product category records in their view, as

How Database Population Works in Odoo 18-cybrosys

In this way, the performance of our Odoo code can be analyzed and tested from the backend.

By the process of data record creation and duplication through database population, the integrity checking and constraints maintenance, like uniqueness, can be carried out by applying variations in the contents of the data.

To read more about how Database Population Works in Odoo 17, refer to our blog How Database Population Works in Odoo 17.


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



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

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

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