Filling the database

odoo-bin populate
August 10, 2023 by
Олена Кирилюк

Instead of exhausting manual or programmatic filling of test data, this feature can be used to populate the database with the desired amount of test data upon request. This can be used to identify various errors or performance issues in the tested streams.

More details: https://www.odoo.com/documentation/15.0/uk/developer/reference/backend/testing.html#database-population

In the module directory, you need to create a directory named populate and within it create files https://__init__.py/ and files that describe the rules for populating the database, a separate file for each model. All these files must be specified in the __init__.py file. And in the https://__init__.py/ file of the main module directory, you need to add "from . import populate".

Properties:


  • Model._populate_sizes 

  • Model._populate_dependencies 

Methods:


  • Model._populate(size)

  • Model._populate_factories()

Filling tools

  • odoo.tools.populate.cartesian(vals, weights=None, seed=False, formatter=<function format_str>, then=None)

  • odoo.tools.populate.compute(function, seed=None)

  • odoo.tools.populate.constant(val, formatter=<function format_str>)

  • odoo.tools.populate.iterate(vals, weights=None, seed=False, formatter=<function format_str>, then=None)

  • odoo.tools.populate.randint(a, b, seed=None)
  • odoo.tools.populate.randomize(vals, weights=None, seed=False, formatter=<function format_str>, counter_offset=0)

Example command to start database filling for Windows:


D:\Projects\Odoo\venv\Scripts\python.exe D:/Projects/Odoo/odoo-bin populate --models=estate.property.tag --size=small -d odoo

Example description:

from odoo.tools import populate
class CustomModel(models.Model)
_inherit = "custom.some_model"
_populate_sizes = {"small": 100, "medium": 2000, "large": 10000}
_populate_dependencies = ["custom.some_other_model"]
def _populate_factories(self):
# Record ids of previously populated models are accessible in the registry
some_other_ids = self.env.registry.populated_models["custom.some_other_model"]
def get_some_field(values=None, random=None, **kwargs):
""" Choose a value for some_field depending on other fields values.
:param dict values:
:param random: seeded :class:`random.Random` object
"""
field_1 = values['field_1']
if field_1 in [value2, value3]:
return random.choice(some_field_values)
return False
return [
("field_1", populate.randomize([value1, value2, value3])),
("field_2", populate.randomize([value_a, value_b], [0.5, 0.5])),
("some_other_id", populate.randomize(some_other_ids)),
("some_field", populate.compute(get_some_field, seed="some_field")),
('active', populate.cartesian([True, False])),
]
def _populate(self, size):
records = super()._populate(size)
# If you want to update the generated records
# E.g setting the parent-child relationships
records.do_something()
return records