Odoo’s framework is built to be modular and developer-friendly, making it easy to extend features, add new functionality, and modify existing business logic. Whether you’re building a new application or enhancing an existing one, understanding how to create custom models and inherit from standard models is fundamental. In Odoo 19, every business object is represented as a model, defined in Python and tightly integrated with views, actions, and data structures.
Creating a Custom Model in Odoo 19
To create a new model, we start by defining a Python class inside the models directory of the module. Odoo automatically maps each model to a database table, making storage and CRUD operations seamless.
Steps to Add a Custom Model
- Create a Python file inside the models folder (for example: models/library_book.py).
- Load the file inside the models package by editing the __init__.py file:
from . import library_book
I.e., from . import filename
- Load the models directory in the module-level __init__.py:
from . import models
- Upgrade the module from the Apps menu.
Once upgraded, you can verify the model under:
Settings > Technical > Database Structure > Models.
What Is a Regular Model?
A regular model is the most common type of model in Odoo.
A regular model in Odoo:
- Creates a physical database table
- Stores data permanently
- Supports all CRUD operations
- Can appear in menus, views, and reports
Let’s create a simple custom model named Library Book.
from odoo import models, fields
class LibraryBook(models.Model):
_name = 'library.book'
_description = 'Library Book'
name = fields.Char(string='Book Name', required=True)
author = fields.Char(string='Author')
published_year = fields.Integer(string='Published Year')
is_available = fields.Boolean(string='Available', default=True)
What this code does
- _name : It creates a new table named library_book in database
- _description: Is like a title or caption for the model, explaining what it represents.
- Fields become columns
Add XML Views for the Model
Create the file: views/library_book_views.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_library_book_form" model="ir.ui.view">
<field name="name">library.book.form</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
<field name="author"/>
<field name="published_year"/>
<field name="is_available"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="action_library_book" model="ir.actions.act_window">
<field name="name">Library Books</field>
<field name="res_model">library.book</field>
<field name="view_mode">list,form</field>
</record>
<!-- Main Menu -->
<menuitem id="menu_library_root"
name="Library"
sequence="10"/>
<!-- Sub Menu -->
<menuitem id="menu_library_book"
name="Books"
parent="menu_library_root"
action="action_library_book"
sequence="20"/>
</odoo>
Once the XML file is included in the manifest, the new menu and views will appear in the UI.
There are also other models in Odoo, namely TransientModel and AbstractModel. TransientModel handles temporary data that exists only during a session, while AbstractModel serves as a blueprint for other models, providing common fields and methods without being instantiated itself.
Inheriting an Existing Model in Odoo 19
Odoo provides inheritance mechanisms that allow developers to extend or customize existing models without modifying core code. The most common mechanism is classical inheritance, defined using _inherit.
Example: Extending res.partner
Create a file: models/res_partner_inherit.py
from odoo import models, fields
class ResPartnerInherit(models.Model):
_inherit = 'res.partner'
membership_number = fields.Char(string='Membership Number')
is_library_member = fields.Boolean(string='Library Member', default=False)
This approach injects new fields into the existing res.partner model, allowing any contact to be used as a library member.
Odoo also provides delegation inheritance and extension inheritance, which allow more advanced reusability patterns. Choosing the right inheritance type ensures your module remains maintainable and easy to upgrade.
Creating custom models and inheriting existing ones is a core part of developing in Odoo 19. With just a few Python and XML files, you can build new business workflows, integrate seamlessly with existing apps, and extend Odoo without touching core code. Understanding when to create a new model and when to inherit an existing one keeps your modules clean, scalable, and compatible with future updates. This flexibility is what makes Odoo such a powerful and developer-friendly ERP framework.
To read more about How to Inherit & Add a Menu to an Existing Model in Odoo 18, refer to our blog How to Inherit & Add a Menu to an Existing Model in Odoo 18.