ORM API - Odoo 14.0 Documentation
ORM API - Odoo 14.0 Documentation
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.
By default, the field’s label (user-visible name) is a capitalized version of the field
name, this can be overridden with the string parameter.
For the list of field types and parameters, see the fields reference.
or as a function called to compute the default value, which should return that value:
def _default_name(self):
return self.get_value()
API
The system automatically instantiates every model once per database. Those
instances represent the available models on each database, and depend on which
modules are installed on that database. The actual class of each instance is built
from the Python classes that create and inherit from the corresponding model.
To create a class that should not be instantiated, the _register attribute may be
set to False.
_auto False
Whether a database table should be created. If set to False , override init()
to create the database table.
Tip
To create a model without any table, inherit from AbstractModel .
_log_access
Whether the ORM should automatically generate and update the Access Log
fields.
_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
registry visibility
_abstract True
Whether the model is abstract.
See also
AbstractModel
_transient False
Whether the model is transient.
See also
TransientModel
_name None
the model name (in dot-notation, module namespace)
_description None
the model’s informal name
_inherit None
Python-inherited models:
Note
If _name is set, name(s) of parent models to inherit from
If _name is unset, name of a single model to extend in-place
_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.
_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 [source]
alias of odoo.models.BaseModel
Model
class odoo.models. Model [source]
Main super-class for regular database-persisted Odoo models.
class user(Model):
...
The system will later instantiate the class once per database (on which the class’
module is installed).
_auto True
Whether a database table should be created. If set to False , override init()
to create the database table.
Tip
To create a model without any table, inherit from AbstractModel .
_abstract False
Whether the model is abstract.
See also
AbstractModel
_transient False
Whether the model is transient.
See also
TransientModel
TransientModel
class odoo.models. TransientModel [source]
Model super-class for transient records, meant to be temporarily persistent, and
regularly vacuum-cleaned.
A TransientModel has a simplified access rights management, all users can create
new records, and may only access the records they created. The superuser has
unrestricted access to all TransientModel records.
_auto True
Whether a database table should be created. If set to False , override init()
to create the database table.
Tip
To create a model without any table, inherit from AbstractModel .
_abstract False
Whether the model is abstract.
See also
AbstractModel
_transient True
Whether the model is transient.
See also
TransientModel
Fields
class odoo.fields. Field [source]
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:
Parame string (str) – the label of the field seen by users; if not set, the ORM takes the
ters: field name in the class (capitalized).
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.
groups (str) – comma-separated list of group xml ids (string); this restricts the
field access to the users of the given groups only
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)
store (bool) – whether the field is stored in database (default: True , False for
computed 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 choices.
@api.model
def _read_group_many2one_field(self, records, domain, order):
return records + self.search([custom_domain])
Computed Fields
See also
Advanced Fields/Compute
fields
See also
Advanced fields/Related
fields
Basic Fields
class odoo.fields. Boolean [source]
Encapsulates a bool .
Parameters: size (int) – the maximum size of values stored for that field
trim (bool) – states whether the value is trimmed or not (by default, True ).
Note that the trim operation is applied only by the web client.
translate (bool or callable) – enable the translation of the field’s values; use
translate=True to translate field values as a whole; translate may also be
a callable such that translate(callback, value) translates value by using
callback(term) to retrieve the translation of terms.
The Float class provides some static methods for this purpose:
round() to round a float with the given precision. is_zero() to check if a float
equals zero at the given precision. compare() to compare two floats at the given
precision.
Example
To round a quantity with the precision of the unit of mesure:
fields.Float.round(self.product_uom_qty, precision_rounding=self.product_uom_id.roundi
To check if the quantity is zero with the precision of the unit of mesure:
fields.Float.is_zero(self.product_uom_qty, precision_rounding=self.product_uom_id.roun
The compare helper uses the __cmp__ semantics for historic purposes, therefore the
proper, idiomatic way to use this helper is like so:
if result == 0, the first and second floats are equal if result < 0, the first float is lower than
the second if result > 0, the first float is greater than the second
Advanced Fields
class odoo.fields. Binary [source]
Encapsulates a binary content (e.g. a file).
If image size is greater than the max_width / max_height limit of pixels, the image
will be resized to the limit by keeping aspect ratio.
Parameters: max_width (int) – the maximum width of the image (default: 0 , no limit)
max_height (int) – the maximum height of the image (default: 0 , no limit)
Note
If no max_width / max_height is specified (or is set to 0 and verify_resolution is False,
the field content won’t be verified at all and a Binary field should be used.
The decimal precision and currency symbol are taken from the currency_field
attribute.
Parameters: currency_field (str) – name of the Many2one field holding the res_currency
this monetary field is expressed in (default: 'currency_id' )
ondelete –
provides a fallback mechanism for any overridden field with a selection_add.
It is a dict that maps every option from the selection_add to a fallback action.
This fallback action will be applied to all records whose selection_add option
maps to it.
The actions can be any of the following:
’set null’ – the default, all records with this option will have their selection
value set to False.
’cascade’ – all records with this option will be deleted along with the option
itself.
’set default’ – all records with this option will be set to the default of the
field definition
<callable> – a callable whose first and only argument will be the set of
records containing the specified Selection option, for custom processing
Parameters: translate (bool or callable) – enable the translation of the field’s values; use
translate=True to translate field values as a whole; translate may also be
a callable such that translate(callback, value) translates value by using
callback(term) to retrieve the 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.
When assigning a value to a Date/Datetime field, the following options are valid:
The Date and Datetime fields class have helper methods to attempt conversion into a
compatible type:
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.
Note
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.
Note
This method may be used to compute default values.
Warning
If a datetime object is given as value, it will be converted to a date object and all
datetime-specific information will be lost (HMS, TZ, …).
Note
This function may be used to compute default values.
Note
This method is not meant for use as a default initializer, because datetime fields are
automatically converted upon display on client side. For default values, now() should
be used instead.
Note
This function may be used to compute default values.
Relational Fields
class odoo.fields. Many2one [source]
The value of such a field is a recordset of size 0 (no record) or 1 (a single record).
Parameters: comodel_name (str) – name of the target model Mandatory except for
related or extended fields.
domain – an optional domain to set on candidate values on the client side
(domain or string)
context (dict) – an optional context to use on the client side when handling
that field
The attributes comodel_name and inverse_name are mandatory except in the case
of related fields or field extensions.
Parameters: comodel_name – name of the target model (string) mandatory except in the
case of related or extended fields
relation (str) – optional name of the table that stores the relation in the
database
column1 (str) – optional name of the column referring to “these” records in
the table relation
column2 (str) – optional name of the column referring to “those” records in
the table relation
The attributes relation , column1 and column2 are optional. If not given, names
are automatically generated from model names, provided model_name and
comodel_name are different!
Note that having several fields with implicit relation parameters on a given model
with the same comodel is not accepted by the ORM, since those field would use
the same table. The ORM prevents two many2many fields to use the same relation
parameters, except if
both fields use the same model, comodel, and relation parameters are explicit;
or
at least one field belongs to a model with _auto = False .
Parameters: domain – an optional domain to set on candidate values on the client side
(domain or string)
context (dict) – an optional context to use on the client side when handling
that field
check_company (bool) Mark the field to be verified in _check_company() .
Add a default company domain depending on the field attributes.
limit (int) – optional limit to use upon read
Pseudo-relational fields
class odoo.fields. Reference [source]
Pseudo-relational field (no FK in database).
Contrary to Reference fields, the model has to be specified in a Char field, whose
name has to be specified in the model_field attribute for the current
Many2oneReference field.
Parameters: model_field (str) – name of the Char where the model name is stored.
Computed Fields
Fields can be computed (instead of read straight from the database) using the
compute parameter. It must assign the computed value to the field. If it uses the
values of other fields, it should specify those fields using depends() .
@api.depends('value', 'tax')
def _compute_total(self):
for record in self:
record.total = record.value + record.value * record.tax
@api.depends('line_ids.value')
def _compute_total(self):
for record in self:
record.total = sum(line.value for line in record.line_ids)
computed fields are not stored by default, they are computed and returned when
requested. Setting store=True will store them in the database and automatically
enable searching.
The search method is invoked when processing domains before doing an actual
search on the model. It must return a domain equivalent to the condition:
field operator value .
def _get_document(self):
for record in self:
with open(record.get_document_path) as f:
record.document = f.read()
def _set_document(self):
for record in self:
if not record.document: continue
with open(record.get_document_path()) as f:
f.write(record.document)
multiple fields can be computed at the same time by the same method, just use
the same method on all fields and set all of them:
discount_value = fields.Float(compute='_apply_discount')
total = fields.Float(compute='_apply_discount')
@api.depends('value', 'discount')
def _apply_discount(self):
for record in self:
# compute actual discount from discount percentage
discount = record.value * record.discount
record.discount_value = discount
record.total = record.value - discount
Warning
While it is possible to use the same compute method for multiple fields, it is not
recommended to do the same for the inverse method.
During the computation of the inverse, all fields that use said inverse are protected, meaning
that they can’t be computed, even if their value is not in the cache.
If any of those fields is accessed and its value is not in cache, the ORM will simply return a
default value of False for these fields. This means that the value of the inverse fields
(other than the one triggering the inverse method) may not give their correct value and this
will probably break the expected behavior of the inverse method.
Related fields
A special case of computed fields are related (proxy) fields, which provide the value
of a sub-field on the current record. They are defined by setting the related
parameter and like regular computed fields they can be stored:
The value of a related field is given by following a sequence of relational fields and
reading a field on the reached model. The complete sequence of fields to traverse is
specified by the related attribute.
Some field attributes are automatically copied from the source field if they are not
redefined: string , help , required (only if all fields in the sequence are required),
groups , digits , size , translate , sanitize , selection , comodel_name , domain
, context . All semantic-free attributes are copied from the source field.
not stored
not copied
readonly
computed in superuser mode
Add the attribute store=True to make it stored, just like computed fields. Related
fields are automatically recomputed when their dependencies are modified.
Tip
You can specify precise field dependencies if you don’t want the related field to be
recomputed on any dependency change:
nickname = fields.Char(
related='partner_id.name', store=True,
depends=['partner_id'])
# The nickname will only be recomputed when the partner_id
# is modified, not when the name is modified on the partner.
Warning
You cannot chain Many2many or One2many fields in related fields dependencies.
related can be used to refer to a One2many or Many2many field on another model on the
condition that it’s done through a Many2one relation on the current model. One2many and
Many2many are not supported and the results will not be aggregated correctly:
m2o_id = fields.Many2one()
m2m_ids = fields.Many2many()
o2m_ids = fields.One2many()
# Supported
d_ids = fields.Many2many(related="m2o_id.m2m_ids")
e_ids = fields.One2many(related="m2o_id.o2m_ids")
Automatic fields
odoo.fields. id
Identifier field
odoo.fields. create_date
Stores when the record was created, Datetime
odoo.fields. create_uid
Stores who created the record, Many2one to a res.users .
odoo.fields. write_date
Stores when the record was last updated, Datetime
odoo.fields. write_uid
Stores who last updated the record, Many2one to a res.users .
Warning
_log_access must be enabled on TransientModel .
odoo.fields. name
default value for _rec_name , used to display records in context where a
representative “naming” is necessary.
Char
odoo.fields. active
toggles the global visibility of the record, if active is set to False the record is
invisible in most searches and listing.
Boolean
odoo.fields. state
lifecycle stages of the object, used by the states attribute on fields .
Selection
odoo.fields. parent_id
default_value of _parent_name , used to organize records in a tree structure and
enables the child_of and parent_of operators in domains.
Many2one
odoo.fields. parent_path
When _parent_store is set to True, used to store a value reflecting the tree
structure of _parent_name , and to optimize the operators child_of and
parent_of in search domains. It must be declared with index=True for proper
operation.
Char
odoo.fields. company_id
Main field name used for Odoo multi-company behavior.
Recordsets
Interactions with models and records are performed through recordsets, an ordered
collection of records of the same model.
Warning
Contrary to what the name implies, it is currently possible for recordsets to contain
duplicates. This may change in the future.
class AModel(models.Model):
_name = 'a.model'
def a_method(self):
# self can be anything between 0 records and all records in the
# database
self.do_operation()
Iterating on a recordset will yield new sets of a single record (“singletons”), much like
iterating on a Python string yields strings of a single characters:
def do_operation(self):
print(self) # => a.model(1, 2, 3, 4, 5)
for record in self:
print(record) # => a.model(1), then a.model(2), then a.model(3), ...
Field access
Recordsets provide an “Active Record” interface: model fields can be read and written
directly from the record as attributes.
Note
When accessing non-relational fields on a recordset of potentially multiple records, use
mapped() :
total_qty = sum(self.mapped('qty'))
Field values can also be accessed like dict items, which is more elegant and safer
than getattr() for dynamic field names. Setting a field’s value triggers an update to
the database:
>>> record.name
Example Name
>>> record.company_id.name
Company Name
>>> record.name = "Bob"
>>> field = "name"
>>> record[field]
Bob
Warning
Trying to read a field on multiple records will raise an error for non relational fields.
To avoid reading one field on one record at a time, Odoo prefetches records and
fields following some heuristics to get good performance. Once a field must be read
on a given record, the ORM actually reads that field on a larger recordset, and stores
the returned values in cache for later use. The prefetched recordset is usually the
recordset from which the record comes by iteration. Moreover, all simple stored fields
(boolean, integer, float, char, text, date, datetime, selection, many2one) are fetched
altogether; they correspond to the columns of the model’s table, and are fetched
efficiently in the same query.
The prefetching also works on secondary records: when relational fields are read,
their values (which are records) are subscribed for future prefetching. Accessing one
of those secondary records prefetches all secondary records from the same model.
This makes the following example generate only two queries, one for partners and
one for countries:
countries = set()
for partner in partners:
country = partner.country_id # first pass prefetches all partners
countries.add(country.name) # first pass prefetches all countries
Method decorators
The Odoo API module defines Odoo Environments and method decorators.
@api.constrains('name', 'description')
def _check_description(self):
for record in self:
if record.name == record.description:
raise ValidationError("Fields name and description must be different
Invoked on the records on which one of the named fields has been modified.
Warning
@constrains only supports simple field names, dotted names (fields of relational fields
e.g. partner_id.customer ) are not supported and will be ignored.
@constrains will be triggered only if the declared fields in the decorated method are
included in the create or write call. It implies that fields not present in a view will not
trigger a call during a record creation. A override of create is necessary to make sure a
constraint will always be triggered (e.g. to test the absence of value).
pname = fields.Char(compute='_compute_pname')
@api.depends('partner_id.name', 'partner_id.is_company')
def _compute_pname(self):
for record in self:
if record.partner_id.is_company:
record.pname = (record.partner_id.name or "").upper()
else:
record.pname = record.partner_id.name
One may also pass a single function as argument. In that case, the dependencies
are given by calling the function with the field’s model.
price = fields.Float(compute='_compute_product_price')
@api.depends_context('pricelist')
def _compute_product_price(self):
for product in self:
if product.env.context.get('pricelist'):
pricelist = self.env['product.pricelist'].browse(product.env.context
else:
pricelist = self.env['product.pricelist'].get_default_pricelist()
product.price = pricelist.get_products_price(product).get(product.id, 0.
All dependencies must be hashable. The following keys have special support:
@api.model
def method(self, args):
...
record = model.create(vals)
records = model.create([vals, ...])
In the form views where the field appears, the method will be called when one of
the given fields is modified. The method is invoked on a pseudo-record that
contains the values present in the form. Field assignments on that record are
automatically sent back to the client.
@api.onchange('partner_id')
def _onchange_partner(self):
self.message = "Dear %s" % (self.partner_id.name or "")
return {
'warning': {'title': "Warning", 'message': "What is this?", 'type': 'notific
}
Warning
@onchange only supports simple field names, dotted names (fields of relational fields e.g.
partner_id.tz ) are not supported and will be ignored
Danger
Since @onchange returns a recordset of pseudo-records, calling any one of the CRUD
methods ( create() , read() , write() , unlink() ) on the aforementioned recordset is
undefined behaviour, as they potentially do not exist in the database yet.
Instead, simply set the record’s field like shown in the example above or call the
update() method.
Warning
It is not possible for a one2many or many2many field to modify itself via onchange. This is
a webclient limitation - see #2693.
The arguments self , *args and **kwargs are the ones passed to the method in
the record-style.
The decorator adapts the method output to the api style: id , ids or False for
the traditional style, and recordset for the record style:
@model
@returns('res.partner')
def find_partner(self, arg):
... # return some record
Environment
The Environment stores various contextual data used by the ORM the database
cursor (for database queries), the current user (for access rights checking) and the
current context (storing arbitrary metadata). The environment also stores caches.
All recordsets have an environment, which is immutable, can be accessed using env
and gives access to:
>>> records.env
<Environment object ...>
>>> records.env.user
res.user(3)
>>> records.env.cr
<Cursor object ...)
When creating a recordset from an other recordset, the environment is inherited. The
environment can be used to get an empty recordset in an other model, and query
that model:
>>> self.env['res.partner']
res.partner()
>>> self.env['res.partner'].search([['is_company', '=', True], ['customer', '=', T
res.partner(7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28,
Environment. lang
Return the current language code.
Environment. user
Return the current user (as an instance).
Environment. company
Return the current company (as an instance).
Warning
No sanity checks applied in sudo mode ! When in sudo mode, a user can access any
company, even if not in his allowed companies.
This allows to trigger inter-company modifications, even if the current user doesn’t have
access to the targeted company.
Environment. companies
Return a recordset of the enabled companies by the user.
Warning
No sanity checks applied in sudo mode ! When in sudo mode, a user can access any
company, even if not in his allowed companies.
This allows to trigger inter-company modifications, even if the current user doesn’t have
access to the targeted company.
The extended context is either the provided context in which overrides are
merged or the current context in which overrides are merged e.g.:
result.env.company = company
result.env.companies = self.env.companies | company
Warning
When using an unauthorized company for current user, accessing the company(ies) on
the environment may trigger an AccessError if not done in a sudoed environment.
Warning
The new environment will not benefit from the current environment’s data cache, so later
data access may incur extra delays while re-fetching from the database. The returned
recordset has the same prefetch object as self .
Returns a new version of this recordset with superuser mode enabled or disabled,
depending on flag . The superuser mode does not change the current user, and
simply bypasses access rights checks.
Warning
Using sudo could cause data access to cross the boundaries of record rules, possibly
mixing records that are meant to be isolated (e.g. records from different companies in
multi-company environments).
It may lead to un-intuitive results in methods which select one record among many - for
example getting the default company, or selecting a Bill of Materials.
Note
Because the record rules and access control will have to be re-evaluated, the new
recordset will not benefit from the current environment’s data cache, so later data access
may incur extra delays while re-fetching from the database. The returned recordset has
the same prefetch object as self .
SQL Execution
The cr attribute on environments is the cursor for the current database transaction
and allows executing SQL directly, either for queries which are difficult to express
using the ORM (e.g. complex joins) or for performance reasons:
self.env.cr.execute("some_sql", params)
Because models use the same cursor and the Environment holds various caches,
these caches must be invalidated when altering the database in raw SQL, or further
uses of models may become incoherent. It is necessary to clear caches when using
CREATE , UPDATE or DELETE in SQL, but not SELECT (which simply reads the
database).
Note
Clearing caches can be performed using the invalidate_cache() method.
Parameters: fnames – the list of modified fields, or None for all fields
ids – the list of modified record ids, or None for all
Warning
Executing raw SQL bypasses the ORM, and by consequent, Odoo security rules. Please
make sure your queries are sanitized when using user input and prefer using ORM utilities if
you don’t really need to use SQL queries.
Create/update
Model. create (vals_list) → records [source]
Creates new records for the model.
The new records are initialized using the values from the list of dicts vals_list ,
and if necessary those from default_get() .
if user tries to bypass access rules for create on the requested object
ValidationError – if user tries to enter invalid value for a field that is not in
selection
UserError – if a loop would be created in a hierarchy of objects a result of the
operation (such as setting an object as its own parent)
Parameters: default (dict) – dictionary of field values to override in the original values of
the copied record, e.g: {'field_name': overridden_value, ...}
Returns: new record
Note
Unrequested defaults won’t be considered, there is no need to return a value for fields
whose names are not in fields_list .
The new record will be initialized with any default values applicable to this model,
or provided through the context. The usual behavior of create() applies.
will set the field foo to 1 and the field bar to "Qux" if those are valid
(otherwise it will trigger an error).
Raises: AccessError –
if user has no write rights on the requested object
if user tries to bypass access rules for write on the requested object
ValidationError – if user tries to enter invalid value for a field that is not in
selection
UserError – if a loop would be created in a hierarchy of objects a result of the
operation (such as setting an object as its own parent)
For numeric fields ( Integer , Float ) the value should be of the corresponding
type
For Selection , the value should match the selection values (generally str ,
sometimes int )
For Many2one , the value should be the database identifier of the record to set
Danger
for historical and compatibility reasons, Date and Datetime fields use strings as
values (written and read) rather than date or datetime . These date strings are
UTC-only and formatted according to odoo.tools.misc.DEFAULT_SERVER_DATE_FORMAT
and odoo.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
(0, 0, values)
adds a new record created from the provided value dict.
(2, id, 0)
removes the record of id id from the set, then deletes it (from the
database). Can not be used in create() .
(3, id, 0)
removes the record of id id from the set, but does not delete it. Can not be
used in create() .
(4, id, 0)
adds an existing record of id id to the set.
(5, 0, 0)
removes all records from the set, equivalent to using the command 3 on
every record explicitly. Can not be used in create() .
(6, 0, ids)
replaces all existing records in the set by the ids list, equivalent to using
the command 5 followed by a command 4 for each id in ids .
Parameters: (list<str>) (fnames) – list of field names to flush. If given, limit the
processing to the given fields of the current model.
Model) (records) – if given (together with fnames ), limit the processing to
the given records.
Search/Read
Model. browse ([ids]) → records [source]
Returns a recordset for the ids provided as parameter in the current environment.
Parameters: args – A search domain. Use an empty list to match all records.
offset (int) – number of results to ignore (default: none)
limit (int) – maximum number of records to return (default: all)
order (str) – sort string
count (bool) – if True, only counts and returns the number of matching
records (default: False)
Returns: at most limit records matching the search criteria
Raises: AccessError –
if user tries to bypass access rules for read on the requested object.
This is used for example to provide suggestions based on a partial value for a
relational field. Sometimes be seen as the inverse function of name_get() , but it is
not guaranteed to be.
Reads the requested fields for the records in self , low-level/RPC method. In
Python code, prefer browse() .
Parameters: domain (list) – A search domain. Use an empty list to match all records.
fields (list) – list of fields present in the list view specified on the object.
Each element is either ‘field’ (field name, using the default aggregation), or
‘field:agg’ (aggregate field with aggregation function ‘agg’), or
‘name:agg(field)’ (aggregate field with ‘agg’ and return it as ‘name’). The
possible aggregation functions are the ones provided by PostgreSQL
(https://www.postgresql.org/docs/current/static/functions-aggregate.html)
and ‘count_distinct’, with the expected meaning.
Fields/Views
Get the detailed composition of the requested view like fields, model, view
architecture
Search domains
A domain is a list of criteria, each criterion being a triple (either a list or a tuple )
of (field_name, operator, value) where:
field_name ( str )
a field name of the current model, or a relationship traversal through a
Many2one using dot-notation e.g. 'street' or 'partner_id.country'
operator ( str )
an operator used to compare the field_name with the value . Valid operators
are:
=
equals to
!=
not equals to
>
greater than
>=
greater than or equal to
<
less than
<=
less than or equal to
=?
unset or equals to (returns true if value is either None or False ,
otherwise behaves like = )
=like
matches field_name against the value pattern. An underscore _ in the
pattern stands for (matches) any single character; a percent sign %
matches any string of zero or more characters.
like
matches field_name against the %value% pattern. Similar to =like but
wraps value with ‘%’ before matching
not like
doesn’t match against the %value% pattern
ilike
case insensitive like
not ilike
case insensitive not like
=ilike
case insensitive =like
in
is equal to any of the items from value , value should be a list of items
not in
is unequal to all of the items from value
child_of
is a child (descendant) of a value record (value can be either one item or a
list of items).
Takes the semantics of the model into account (i.e following the relationship
field named by _parent_name ).
parent_of
is a parent (ascendant) of a value record (value can be either one item or a
list of items).
Takes the semantics of the model into account (i.e following the relationship
field named by _parent_name ).
value
variable type, must be comparable (through operator ) to the named field.
'&'
logical AND, default operation to combine criteria following one another. Arity 2
(uses the next 2 criteria or combinations).
'|'
logical OR, arity 2.
'!'
logical NOT, arity 1.
Note
Mostly to negate combinations of criteria Individual criterion generally have a negative
form (e.g. = != , < >= ) which is simpler than negating the positive.
Example
To search for partners named ABC, from belgium or germany, whose language is not
english:
[('name','=','ABC'),
('language.code','!=','en_US'),
'|',('country_id.code','=','be'),
('country_id.code','=','de')]
(name is 'ABC')
AND (language is NOT english)
AND (country is Belgium OR Germany)
Unlink
Model. unlink () [source]
Deletes the records of the current set
Raises: AccessError –
if user has no unlink rights on the requested object
if user tries to bypass access rules for unlink on the requested object
Record(set) information
Model. ids
Return the list of actual record ids corresponding to self .
odoo.models. env
Returns the environment of the given recordset.
Type: Environment
if record.exists():
...
xmlid: XML ID to use to refer to this record (if there is one), in format
module.name
Operations
Recordsets are immutable, but sets of the same model can be combined using
various set operations, returning new recordsets.
Recordsets are iterable so the usual Python tools are available for transformation (
map() , sorted() , itertools.ifilter , …) however these return either a list or an
iterator, removing the ability to call methods on their result, or to use set operations.
Filter
Map
# returns a list of summing two fields for each record in the set
records.mapped(lambda r: r.field1 + r.field2)
Note
Since V13, multi-relational field access is supported and works like a mapped call:
records.partner_id # == records.mapped('partner_id')
records.partner_id.bank_ids # == records.mapped('partner_id.bank_ids')
records.partner_id.mapped('name') # == records.mapped('partner_id.name')
Sort
Parameters: key (callable or str or None) – either a function of one argument that returns
a comparison key for each record, or a field name, or None , in which case
records are ordered according the default model’s order
reverse (bool) – if True , return the result in reverse order
creating a new model from an existing one, adding new information to the copy
but leaving the original module as-is
extending models defined in other modules in-place, replacing the previous
version
delegating some of the model’s fields to records it contains
Classical inheritance
When using the _inherit and _name attributes together, Odoo creates a new
model using the existing one (provided via _inherit ) as a base. The new model gets
all the fields, methods and meta-information (defaults & al) from its base.
class Inheritance0(models.Model):
_name = 'inheritance.0'
_description = 'Inheritance Zero'
name = fields.Char()
def call(self):
return self.check("model 0")
class Inheritance1(models.Model):
_name = 'inheritance.1'
_inherit = 'inheritance.0'
_description = 'Inheritance One'
def call(self):
return self.check("model 1")
a = env['inheritance.0'].create({'name': 'A'})
b = env['inheritance.1'].create({'name': 'B'})
a.call()
b.call()
will yield:
the second model has inherited from the first model’s check method and its name
field, but overridden the call method, as when using standard Python inheritance.
Extension
When using _inherit but leaving out _name , the new model replaces the existing
one, essentially extending it in-place. This is useful to add new fields or methods to
existing models (created in other modules), or to customize or reconfigure them (e.g.
to change their default sort order):
class Extension0(models.Model):
_name = 'extension.0'
_description = 'Extension zero'
name = fields.Char(default="A")
class Extension1(models.Model):
_inherit = 'extension.0'
description = fields.Char(default="Extended")
record = env['extension.0'].create({})
record.read()[0]
will yield:
Note
It will also yield the various automatic fields unless they’ve been disabled
Delegation
The third inheritance mechanism provides more flexibility (it can be altered at
runtime) but less power: using the _inherits a model delegates the lookup of any
field not found on the current model to “children” models. The delegation is
performed via Reference fields automatically set up on the parent model.
The main difference is in the meaning. When using Delegation, the model has one
instead of is one, turning the relationship in a composition instead of inheritance:
class Screen(models.Model):
_name = 'delegation.screen'
_description = 'Screen'
class Keyboard(models.Model):
_name = 'delegation.keyboard'
_description = 'Keyboard'
layout = fields.Char(string='Layout')
class Laptop(models.Model):
_name = 'delegation.laptop'
_description = 'Laptop'
_inherits = {
'delegation.screen': 'screen_id',
'delegation.keyboard': 'keyboard_id',
}
name = fields.Char(string='Name')
maker = fields.Char(string='Maker')
record = env['delegation.laptop'].create({
'screen_id': env['delegation.screen'].create({'size': 13.0}).id,
'keyboard_id': env['delegation.keyboard'].create({'layout': 'QWERTY'}).id,
})
record.size
record.layout
13.0
'QWERTY'
record.write({'size': 14.0})
Warning
when using delegation inheritance, methods are not inherited, only fields
Warning
_inherits is more or less implemented, avoid it if you can;
chained _inherits is essentially not implemented, we cannot guarantee anything on
the final behavior.
For instance, the second class below only adds a tooltip on the field state :
class First(models.Model):
_name = 'foo'
state = fields.Selection([...], required=True)
class Second(models.Model):
_inherit = 'foo'
state = fields.Selection(help="Blah blah blah")
Error management
The Odoo Exceptions module defines a few core exception types.
Those types are understood by the RPC layer. Any other exception type bubbling
until the RPC layer will be treated as a ‘Server error’.
Note
If you consider introducing new exceptions, check out the odoo.addons.test_exceptions
module.
Note
No traceback.
Example
When you try to log with a wrong password.
Example
When you try to read a record that you are not allowed to.
Example
When you try to read a value in a flushed cache.
Example
When you try to write on a deleted record.
Typically when the user tries to do something that has no sense given the current
state of a record. Semantically comparable to the generic 400 HTTP status codes.
Example
When you try to create a new user with a login which already exist in the db.
Get Help