Hello Odooers & Developers! Welcome to another technical article on Odoo! In this guide, we'll learn how to create a custom object (model) in Odoo 18 step-by-step.
Creating your model is one of the most essential parts of Odoo development. Models are like the building blocks of any Odoo app. They define what kind of data you store and how you manage it.
What You Will Learn in This Guide:
- What is a Model/Object in Odoo?
- How to Define a Model in Python
- How to Add Fields to the Model
- How to Create Views (Form & Tree)
- How to Make the Object Visible in the Odoo Menu
What is a Model (Object) in Odoo?
A model in Odoo is a Python class representing a business object like Customer, Invoice, Task, Product, etc. Models in Odoo are defined using the models. Model class and interact with the PostgreSQL database using the Odoo ORM.
For example, if you're creating a Book Management module, you could define a model like a library. Book.
Step-by-Step: Create a Custom Object in Odoo
Let’s say we want to create a custom object called a Library Book.
1. Define Your Model in Python
Create a new module or use an existing custom module. In the models folder, create a Python file like library_book.py.
.py
from odoo import models, fields
class LibraryBook(models.Model):
_name = 'library.book'
_description = 'Library Book'
name = fields.Char(string='Title', required=True)
author = fields.Char(string='Author')
date_published = fields.Date(string='Published Date')
isbn = fields.Char(string='ISBN')
pages = fields.Integer(string='Number of Pages')
Here:
- _name is the technical name of the object.
fields.Char, fields.Date, etc., define the structure of your object.
2. Add the Model to __init__.py
Ensure your model file is imported in your_module/models/__init__.py:
.py
from . import library_book
Inside your_module/__init__.py
Make sure you import the whole models directory:
.py
from . import models
Why This Is Important:
- The root __init__.py tells Odoo to look into the models/ folder.
- The models/__init__.py file tells Odoo which specific model files to import.
Without these, your models won’t be recognized or loaded!
3. Declare It in the Manifest File
Add 'models' to the manifest.py:
.py
'data': [
'security/ir.model.access.csv',
'views/library_book_views.xml',
],
4. Create Views (Tree & Form)
Create a file: views/library_book_views.xml
XML
<odoo>
<!--Form View →
<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 string="Library Book">
<sheet>
<group>
<field name="name"/>
<field name="author"/>
<field name="date_published"/>
<field name="isbn"/>
<field name="pages"/>
</group>
</sheet>
</form>
</field>
</record>
<!--List View →
<record id="view_library_book_tree" model="ir.ui.view">
<field name="name">library.book.tree</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<list string="Library Books">
<field name="name"/>
<field name="author"/>
<field name="date_published"/>
</list>
</field>
</record>
</odoo>
5. Add Menu & Action
You’ll need a menu item and action to open your model in the UI.
XML
<odoo>
<!-- Window Action -->
<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>
<!-- Root Menu -->
<menuitem id="menu_library_root" name="Library"/>
<!-- Sub Menu -->
<menuitem id="menu_library_book" name="Books" parent="menu_library_root" action="action_library_book"/>
</odoo>
Explanation & Flow
1. Action (ir.actions.act_window)
- This defines what Odoo should open when the menu is clicked.
- It links the library.book model and sets it to open with both list (tree) and form views.
2. Menu Root (menu_library_root)
- Creates a top-level menu item named "Library" that appears in the main menu bar.
3. Menu Item (menu_library_book)
- Adds a submenu called "Books" under "Library".
- It connects to the action defined above, so when clicked, it opens the list/form view of library.book.
6. Set Access Rights
In security/ir.model.access.csv:
.csv
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_library_book,access.library.book,model_library_book,,1,1,1,1
Explanation:
access_library_book : A unique ID for this access control rule (technical name).
access.library.book : Descriptive name for the rule. It can be anything readable.
model_library_book : Technical reference to the model. It links to the model defined in Python (_name = 'library.book'). Odoo auto-generates model_<model_name> XML IDs for models.
group_id:id(empty) : If left empty, this rule applies to all users. You can restrict this rule to a specific user group (e.g., base.group_user) by filling this field.
perm_read - 1 : Allows users to view/read records.
perm_write - 1 : Allows users to edit/update existing records.
perm_create - 1 : Allows users to create new records.
perm_unlink - 1 : Allows users to delete records.
Restart & Upgrade Your Module
After coding:
- Restart the Odoo server.
- Upgrade your module from the Apps menu (activate Developer Mode first).
Conclusion:
This XML code helps you show your custom model in the Odoo interface so users can open it from the menu and use it easily.
It adds a new menu section ("Library") and a submenu ("Books") and connects the submenu to a defined action.
The action opens the library. Book records are in a tree view by default, with the ability to open the form view on a record click.
The library. Book models typically store book information, such as titles, authors, and publication details.
It is the central object for managing book records in the library module.
With just these few lines of code, your model will appear in the Odoo menu. This is an important step in making your backend work visible and usable for people through the Odoo interface.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
If you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.