Hexadecimal Mobile Logo
Open Menu

How Database Population Works in Odoo 18

How Database Population Works in Odoo 18

Image Source: google

Database population is essential to make an Odoo instance usable with predefined or test data. Odoo 18 provides several mechanisms to populate data, including models, XML records, Python scripts, and CSV file imports. Understanding these methods helps developers and administrators quickly set up and configure Odoo for their specific needs.

Understanding the Odoo Database Structure

Odoo is built on top of PostgreSQL, where every model you define in Odoo corresponds to a database table. Each field in a model becomes a column in that table.

Example:

  • A res.partner model creates a res_partner table.
  • Each record is stored as a row with its field values.

Defining a Data Model

Before populating data, you must define a model to store information. Here's an example of a "Product Catalog" model:

python
from odoo import models, fields

class ProductCatalog(models.Model):
    _name = 'custom.product.catalog'
    _description = 'Product Catalog'
    
    name = fields.Char(string='Product Name')
    price = fields.Float(string='Price')
    stock = fields.Integer(string='Stock Quantity')
  • This model creates a table named custom_product_catalog in the PostgreSQL database.

Need expert help setting up your Odoo database? We can help.

Need expert help setting up your Odoo database? We can help.

Talk to Our ExpertsArrow

Populating the Database with XML Files

Odoo supports preloading data using XML files. These files define records that are automatically inserted into the database when the module is installed.

Example XML:

<odoo>
    <record id="product_apple" model="custom.product.catalog">
        <field name="name">Apple</field>
        <field name="price">1.5</field>
        <field name="stock">100</field>
    </record>
    
    <record id="product_orange" model="custom.product.catalog">
        <field name="name">Orange</field>
        <field name="price">2.0</field>
        <field name="stock">80</field>
    </record>
</odoo>

  • Place this file in the data/ folder of your module.

  • Add it in manifest.py under 'data': ['data/product_data.xml'].

You Might Also Like

Populating Data with Python Code

You can also programmatically create records using Python during module installation.

Example:


from odoo import api, SUPERUSER_ID

def populate_data(env):
    env['custom.product.catalog'].create({
        'name': 'Banana',
        'price': 1.0,
        'stock': 120
    })

  • Add this to a post-init hook or custom script to insert data dynamically.
Want to automate data population in Odoo 18? Let us set it up for you.

Want to automate data population in Odoo 18? Let us set it up for you.

Explore Our ServicesArrow

Using CSV Files for Bulk Data Import

Odoo allows importing large datasets using CSV files from both the user interface and modules.

Example product_data.csv:


id,name,price,stock
product_grapes, Grapes, 2.5, 50
product_mango, Mango, 3.0, 60

Define it in your manifest:


'data': [
    'data/product_data.csv'
]
  • Odoo maps CSV columns to field names in the model and populates the database.

FAQs

Q1. What is database population in Odoo 18?
A: It is the process of inserting initial or demo data into Odoo’s database tables via XML, Python scripts, or CSV files.

Q2. Can I populate data without coding?
A: Yes, you can import data directly through the Odoo interface using CSV or Excel files without writing code.

Q3. Which is the best method for demo data?
A: XML data files are commonly used for loading predefined demo data during module installation.

Q4. Can I update database records using XML files?
A: Yes, you can update existing records by using the same record id in the XML file.

Q5. Does Odoo automatically create tables in the database?
A: Yes, once you define a model in Python and install the module, Odoo automatically creates the corresponding PostgreSQL table.

Scroll to top arrow
Grid background

Buy, Sell & Rent Properties – Download HexaHome App Now!

  • Search Icon

    Find your perfect home

  • House Icon

    Post your property at ₹0

Available on iOS & Android

download-playstoredownload-ios
mobile-app-banner

A Product By Hexadecimal Software Pvt. Ltd.