ORM API - Odoo 13.0 Documentation
ORM API - Odoo 13.0 Documentation
Navigate %
Models
Model fields are defined as attributes on the
model itself:
field1 = fields.Char()
! Warning
this means you cannot define a field and a
method with the same name, the last one will
silently overwrite the former ones.
def _default_name(self):
return self.get_value()
API
class odoo.models.BaseModel
Base class for Odoo models.
_auto = False
Whether a database table should be
created (default: True ). If set to False ,
override init() to create the database
table.
_table = None
SQL table name used by model if _auto
_sequence = None
SQL sequence to use for ID field
_sql_constraints = []
SQL constraints [(name, sql_def,
message)]
_register = True
not visible in ORM registry
_name = None
the model name (in dot-notation, module
namespace)
_description = None
the model’s informal name
_inherit = None
Python-inherited models:
Type:
str or list(str)
_inherits = {}
dictionary {‘parent_model’: ‘m2o_field’}
mapping the _name of the parent
business objects to the names of the
corresponding foreign key fields to use:
_inherits = {
'a.model': 'a_field_id',
'b.model': 'b_field_id'
}
implements composition-based
inheritance: the new model exposes all
the fields of the inherited models but
stores none of them: the values
themselves remain stored on the linked
record.
! Warning
if multiple fields with the same name are
defined in the _inherits -ed models, the
inherited field will correspond to the last
one (in the inherits list order).
_rec_name = None
field to use for labeling records, default:
name
_order = 'id'
default order field for searching results
_check_company_auto = False
On write and create, call _check_company
to ensure companies consistency on the
relational fields having
check_company=True as attribute.
_parent_name = 'parent_id'
the many2one field used as parent field
_parent_store = False
set to True to compute parent_path field.
_abstract = True
whether model is abstract
_transient = False
whether model is transient
_date_name = 'date'
field to use for default calendar view
_fold_name = 'fold'
field to determine folded groups in
kanban views
AbstractModel
odoo.models.AbstractModel
alias of odoo.models.BaseModel
Model
class odoo.models.Model
Main super-class for regular database-
persisted Odoo models.
class user(Model):
...
TransientModel
class odoo.models.TransientModel
Model super-class for transient records,
meant to be temporarily persistent, and
regularly vacuum-cleaned.
Fields
class odoo.fields.Field
The field descriptor contains the field
definition, and manages accesses and
assignments of the corresponding field on
records. The following attributes may be
provided when instanciating a field:
Parameters:
string ( str ) – the label of the field seen by users; if not set, the
ORM takes the field name in the class (capitalized).
readonly ( bool ) –
whether the field is readonly (default: False )
This only has an impact on the UI. Any field assignation in code will
work (if the field is a stored field or an inversable one).
default ( value or callable ) – the default value for the field; this
is either a static value, or a function taking a recordset and returning
a value; use default=None to discard default values for the field
states ( dict ) –
a dictionary mapping state values to lists of UI attribute-value pairs;
possible attributes are: readonly , required , invisible .
! Warning
Any state-based condition requires the state field value
to be available on the client-side UI. This is typically done
by including it in the relevant views, possibly made
invisible if not relevant for the end-user.
company_dependent ( bool ) –
whether the field value is dependent of the current company;
The value isn’t stored on the model table. It is registered as
ir.property . When the value of the company_dependent field is
needed, an ir.property is searched, linked to the current company
(and current record if one property exists).
If the value is changed on the record, it either modifies the existing
property for the current record (if one exists), or creates a new one
for the current company and res_id.
If the value is changed on the company side, it will impact all records
on which the value hasn’t been changed.
copy ( bool ) – whether the field value should be copied when the
record is duplicated (default: True for normal fields, False for
one2many and computed fields, including property fields and related
fields)
group_operator ( str ) –
aggregate function used by read_group() when grouping on this
field.
Supported aggregate functions are:
array_agg : values, including nulls, concatenated into an array
group_expand ( str ) –
function used to expand read_group results when grouping on the
current field.
@api.model
def _read_group_selection_field(self, values, domain, order):
return ['choice1', 'choice2', ...] # available selection choi
ces.
@api.model
def _read_group_many2one_field(self, records, domain, order):
return records + self.search([custom_domain])
Computed Fields
Parameters:
compute ( str ) –
name of a method that computes the field
related ( str ) –
sequence of field names
Basic Fields
class odoo.fields.Boolean
Encapsulates a bool .
class odoo.fields.Char
Basic string field, can be length-limited,
usually displayed as a single-line string in
clients.
Parameters:
class odoo.fields.Float
Encapsulates a float .
Parameters:
class odoo.fields.Integer
Encapsulates an int .
Advanced Fields
class odoo.fields.Binary
Encapsulates a binary content (e.g. a file).
Parameters:
class odoo.fields.Html
Encapsulates an html code content.
Parameters:
class odoo.fields.Image
Encapsulates an image, extending Binary .
Parameters:
class odoo.fields.Monetary
Encapsulates a float expressed in a given
res_currency .
Parameters:
class odoo.fields.Selection
Encapsulates an exclusive choice between
different values.
Parameters:
selection_add
( list ( tuple ( str , str ) ) ) –
provides an extension of the selection in the case
of an overridden field. It is a list of pairs (value,
label) or singletons (value,) , where singleton
values must appear in the overridden selection.
The new values are inserted in an order that is
consistent with the overridden selection and this
list:
class odoo.fields.Text
Very similar to Char but used for longer
contents, does not have a size and usually
displayed as a multiline text box.
Parameters:
translation of terms.
Date(time) Fields
Dates and Datetimes are very important fields
in any kind of business application. Their
misuse can create invisible yet painful bugs,
this section aims to provide Odoo developers
with the knowledge required to avoid misusing
these fields.
# Example
To parse date/datetimes coming from external
sources:
fields.Date.to_date(self._context.get('date
_from'))
! Warning
Strings representing dates and datetimes can
be compared between each other, however the
result may not be the expected result, as a
datetime string will always be greater than a
date string, therefore this practice is heavily
discouraged.
Timezones
Datetime fields are stored as timestamp without
timezone columns in the database and are
stored in the UTC timezone. This is by design,
as it makes the Odoo database independent
from the timezone of the hosting server system.
Timezone conversion is managed entirely by the
client side.
class odoo.fields.Date
Encapsulates a python date object.
Parameters:
value – initial date or datetime.
Returns:
static context_today(timestamp=None)
Return the current date as seen in the
client’s timezone in a format fit for date
fields.
Parameters:
record – recordset from which the timezone
will be obtained.
Return type:
date
static end_of(granularity)
Get end of a time period from a date or a
datetime.
Parameters:
value – initial date or datetime.