Odoo Custom Module Development Part 2 - Models and Fields
This is the second article in the Odoo custom module development series.
In Part 1, we created the estate module skeleton and made Odoo recognize it. In this article, we will create our first Odoo model and add useful fields.
Goal of this article
We want to create a model named estate.property that stores:
- property name
- description
- expected price
- selling price
- postcode
- availability date
- active flag
- state
1. Create the model file
Inside the estate module, create a Python file:
estate/ |
Add this to models/__init__.py:
from . import property |
Now create models/property.py:
from odoo import fields, models |
2. Understand what this code does
Important pieces:
_name: the technical model name in Odoo_description: readable model descriptionfields.Char(): short textfields.Text(): long textfields.Float(): decimal numeric fieldfields.Boolean(): true or falsefields.Selection(): dropdown values
The model name must be unique across Odoo.
3. Upgrade the module
Whenever you add or modify models, upgrade the module so Odoo updates the database schema.
From the Odoo source directory:
cd ~/odoo-dev/odoo |
If the command finishes without errors, the database table for estate.property has been created.
4. How Odoo maps the model to the database
Odoo automatically creates a PostgreSQL table for the model. For estate.property, the generated table name is usually:
estate_property |
You do not need to create SQL tables manually for standard models. Odoo’s ORM handles that for you.
5. Add default values for garden fields
If a property has a garden, it often makes sense to provide default values. Update the model like this:
from odoo import fields, models |
Upgrade the module again after editing.
6. Add a second model for property types
Most real estate systems need property categories such as apartment, villa, or office. Create another model file named property_type.py.
Add this line to models/__init__.py:
from . import property_type |
Create models/property_type.py:
from odoo import fields, models |
Upgrade the module again.
7. Good model design habits
When defining models, keep these practices in mind:
- use clear technical model names like
estate.property - make required business data explicitly
required=True - use
copy=Falsefor values that should not duplicate to copied records - give sensible defaults where it improves the data entry workflow
- use
_orderto make record listings predictable
8. Common mistakes
You edited Python files but nothing changed
You probably forgot to upgrade the module:
python3 odoo-bin -c ~/odoo-dev/odoo.conf -d odoo19 -u estate --stop-after-init |
A field type was changed and Odoo throws an error
Changing a field type after data already exists can require a migration. During early development, it is often faster to use a fresh test database.
Odoo says the model does not exist
Check:
- the file is imported in
models/__init__.py - the module itself is imported in the root
__init__.py - the Python syntax is valid
The root __init__.py should contain:
from . import models |
Final words
You now have real models and fields managed by Odoo’s ORM.
In the next article, we will add security so users can actually access these models safely.
Previous article: Part 1 - Create Your First Module
Next article: Part 3 - Security and Access Rights
Related posts
- /article/odoo-custom-module-development-part-1-create-your-first-module/
- /article/odoo-custom-module-development-part-5-relations-computed-fields-and-business-logic/
- /article/odoo-custom-module-development-part-3-security-and-access-rights/
- /article/odoo-custom-module-development-part-4-menus-actions-and-views/
- /article/odoo-custom-module-development-part-6-reports-demo-data-and-tests/