Understand Odoo Model Part 2
1 Fields in Odoo Model
A model represents a business entity that is stored in a database.
Fields represent entity properties and the corresponding table
columns in the database. Odoo also uses
fields representing entity relationships.
Odoo 8.0 introduces a new field syntax but keeps the old
syntax for backward compatibility. Here we focus on the
new syntax defined in
Field class is the base class for all other field
types. It has a meta class called
MetaField. This is
the place we start with.
MetaField Meta Class
This class defines one class variable, a dict called
and a method
__init__ that is called for every new field
by_type is used in
__init__ to create a map of a
field type and its class:
cls.by_type[cls.type] = cls.
__init__ method, after create a field type and
its class map, it creates three lists as class attributes:
In all lists, an element is a tuple of (name_without_prefix,
name_with_prefix) where the prefix is
_description_ in an attribute name.
Field Base Class
3.1 Field Meta Data
Field class uses some attributes to store field meta data.
Understanding these data structure and their usage helps
understanding the class.
self._attrs is a dict that keeps all
field attributes provided by
self._free_attrs is a list of attributes besides
that are added by inheritance and setup.
self._triggers is a set of pairs
(field, path) that
represents the computed fields that depend on
self is modified, it invalidates the cache of each
field, and registers the records to recompute base on
self.depends is a collection of field dependencies.
inverse_fields is a lis tof inverse fields.
_columns is a dictionary that stores old-style
fields for fields whose
This method is called in
_add_field method of
to setup a field when a model add field definitions.
During setup, it first sets
self.model_name to model name and
self.name to field name. Then it put all
inherited attributes into a dict variable
compute attribute is defined, set
to their default for
attrs if they are not defined in construction.
related attribute is defined, set
if is not defined in construction.
column is not an instance of
store attribute from
For attributes defined in
attrs but not in
self, put it to
self._free_attrs. Copy all
attrs attributes as
related attribute are not defined, assign
it to a formatted name value. A related field get its string
from its parent field.
Then it set
Finally it calls
reset to prepare
self for additional setup.
reset method sets
self._triggers to an empty set, and sets
inverse_fields to an empty list.
Selection class override this method
setup method is called for every field in a model’s
setup_models is called in the
create method of
ir_model and in
load_module_graph method in
setup method calls
_setup method and set
_setup method calls
_setup_related for related field
_setup_regular for regular field. Then it calls
_setup_dependency for paths in
self.depends. Finally it put
invalidation triggers on model dependencies for recomputing purpose.
_setup_related method, it sets self attributes from related field
attributes. It sets
True if all related fields
_setup_regular method, it first set
Then it sets
self.dependents property return computed fields that depend on
It returns a dict whose key:value pairs are from 1)
deprecated from a model instance’s
help from local methods.
The docstring of this method is strange:
return a low-level field object corresponding to
In a sense it creates an old-style field with a set of column-related
3.6 The Descriptor Method
Field instance is a data descriptor because the
__set__ methods. In
it first tries model cache, if fails then it calls
to retrieve a value from a database. For a draft record, i.e.,
a record without
id, it calls
__set__ method, it first convert the value to cache value, then
for normal data, call its model’s
write method For draft record,
modified_draft, inverse field and mark record as dirty.
This method finds the value of
self for a record.
If this is a stored field, call
record._prefetch_field(self) to read
the field value from database using
record.read method. It then puts
read value in cache. When it gets a field value, it gets value for
all ids in the
env.preftch_ids that don’t have a cache for this field.
If it is a compute file, it calls
During recomputing, it sets env to
draft mode. When it is done,
it sets the mode to
False. For fields in
_inherits, the field’s
related attribute has a value of
_compute_related to get value from the parent table.