Models and Basic Fields in Odoo

Muhammad Abdullah Arif
4 min readJun 5, 2024

--

Models and Basic Fields in Odoo

At the end of the previous chapter, we were able to create an Odoo module. However, at this point, it is still an empty shell that doesn’t allow us to store any data. In our real estate module, we want to store information related to properties (name, description, price, living area, etc.) in a database. The Odoo framework provides tools to facilitate these database interactions.

Before moving forward in this exercise, make sure the estate module is installed. It should appear as ‘Installed’ in the Apps list.

Warning: Avoid Mutable Global Variables

A single Odoo instance can run several databases in parallel within the same Python process. Different modules might be installed on each of these databases, so we cannot rely on global variables that might be updated depending on installed modules.

Object-Relational Mapping (ORM)

A key component of Odoo is the ORM layer, which helps avoid manually writing most SQL and provides extensibility and security services.

Business objects are declared as Python classes extending Model, integrating them into the automated persistence system. Models can be configured by setting attributes in their definition. The most important attribute is _name, which is required and defines the name for the model in the Odoo system.

Here is a minimal definition of a model:

from odoo import models

class TestModel(models.Model):
_name = "test_model"

This definition is enough for the ORM to generate a database table named test_model. By convention, all models are located in a models directory, and each model is defined in its own Python file.

Exercise: Define the Real Estate Properties Model

Based on the example given in the CRM module, create the appropriate files and folder for the estate_property table.

Steps:

  1. Create Files and Folders:
  • Define the model in the file estate/models/estate_property.py.
  • Import the file in estate/models/__init__.py.
  • Import the models folder in estate/__init__.py.

2. Add Minimum Definition:

from odoo import models, fields 
class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property"

3. Restart Odoo Server:

./odoo-bin --addons-path=addons,../enterprise/,../tutorials/ -d rd-demo -u estate

During the startup, you should see the following warnings:

WARNING rd-demo odoo.models: The model estate.property has no _description
WARNING rd-demo odoo.modules.loading: The model estate.property has no access rules, consider adding one...

If this is the case, then you should be good! To be sure, double-check with psql as demonstrated in the Goal section.

Exercise: Add a Description

Add a _description to your model to get rid of one of the warnings:

class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property"

Model Fields

Fields define what the model can store and where they are stored. Fields are defined as attributes in the model class:

from odoo import fields, models
class TestModel(models.Model):
_name = "test_model"
_description = "Test Model"
name = fields.Char()

The name field is a Char, represented as a Python unicode str and a SQL VARCHAR.

Exercise: Add Basic Fields to the Real Estate Property Table

Add the following basic fields to the table:

FieldTypenameChardescriptionTextpostcodeChardate_availabilityDateexpected_priceFloatselling_priceFloatbedroomsIntegerliving_areaIntegerfacadesIntegergarageBooleangardenBooleangarden_areaIntegergarden_orientationSelection

The garden_orientation field must have 4 possible values: ‘North’, ‘South’, ‘East’, and ‘West’.

Steps:

  1. Define Fields:
class EstateProperty(models.Model):     
_name = "estate.property"
_description = "Real Estate Property"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date()
expected_price = fields.Float(required=True)
selling_price = fields.Float()
bedrooms = fields.Integer()
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection([
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West'),
])
  1. Restart Odoo Server:
  • ./odoo-bin --addons-path=addons,../enterprise/,../tutorials/ -d rd-demo -u estate
  1. Verify Table Structure: Connect to psql and check the structure of the table estate_property:
  • \d estate_property

Exercise: Set Attributes for Existing Fields

Add the following attributes:

  • Field
  • Attribute
  • name
  • required
  • expected_price
  • required
  • Update the model definition:
class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date()
expected_price = fields.Float(required=True)
selling_price = fields.Float()
bedrooms = fields.Integer()
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection([
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West'),
])

After restarting the server, both fields should be non-nullable.

Automatic Fields

Odoo automatically adds a few fields to all models. These fields are managed by the system and can’t be written to, but they can be read if necessary:

  • id: The unique identifier for a record of the model.
  • create_date: Creation date of the record.
  • create_uid: User who created the record.
  • write_date: Last modification date of the record.
  • write_uid: User who last modified the record.

Now that we have created our first model, let’s add some security in the next chapter!

Click on the link below to discover a wealth of knowledge and explore a variety of engaging topics.

Medium Profile: Muhammad Abdullah Arif — Medium

Stay Up-to-Date with Muhammad Abdullah Arif’s Latest Publications — Subscribe Now! (medium.com)

If you wish to offer your support, kindly click on the link below to treat me to a coffee! ☕️😊

https://www.buymeacoffee.com/smuhabdullah

I wish you all the best in your future endeavours.

--

--

Muhammad Abdullah Arif

Python developer. The facts are the facts but opinions are my own.