ORM fields types
• string: field label (required)
• required: True if mandatory
• readonly: True if not editable
• help: help tooltip
• select: True to create a
database index on this column
• context: dictionary with contextual
parameters (for relational fields)
• change_default: True if field should be usable
as condition for default values in clients
• states: dynamic changes to this field's
common attributes based on the state field
(→42,46)
Simple fields
boolean(...) integer(...) date(...)
datetime(...) time(...)
'active': fields.boolean('Active'),
'priority': fields.integer('Priority'),
'start_date': fields.date('Start Date'),
char(string,size,translate=False,..)
text(string, translate=False, …)
Text-based fields
• translate: True if field values can be
translated by users, for char/text fields
• size: optional max size for char fields
(→41,45)
float(string, digits=None, ...)
Decimal value
• digits: tuple (precision, scale) (→58)
selection(values, string, ...)
Field allowing selection among
a set of predefined values
• values: list of values (key-label tuples) or
function returning such a list (required) (→42)
binary(string, filters=None, ...)
Field for storing a file or binary
content.
• filters: optional filename filters for selection
'picture': fields.binary('Picture',
filters='*.png,*.gif')
reference(string, selection, size,..)
Field with dynamic relationship
to any other object, associated
with an assistant widget
• selection: model _name of allowed objects
types and corresponding label (same format as
values for selection fields) (required)
• size: size of text column used to store it
(storage format is 'model_name,object_id')
'contact': fields.reference('Contact',[
('res.partner','Partner'),
('res.partner.contact','Contact')])
Relational fields
Common attributes supported by
relational fields
• domain: optional filter in the form of
arguments for search (see search())
many2one(obj, ondelete='set null', …)
(→50)
Relationship towards a parent
object (using a foreign key)
• obj: _name of destination object (required)
• ondelete: deletion handling, e.g. 'set null',
'cascade', see PostgreSQL documentation
one2many(obj, field_id, …) (→55)
Virtual relationship towards
multiple objects (inverse of
many2one)
• obj: _name of destination object (required)
• field_id: field name of inverse many2one, i.e.
corresponding foreign key (required)
many2many(obj, rel, field1, field2, …)
(→56)
Bidirectional multiple
relationship between objects
• obj: _name of destination object (required)
• rel: optional name of relationship table to use
(default: auto-assigned based on model names)
• field1: name of field in rel table storing the id
of the current object (default: based on model)
• field2: name of field in rel table storing the id
of the target object (default: based on model)
Functional fields
ORM fields types
function(fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float',
fnct_search=None, obj=None, store=False, multi=False,…)
Functional field simulating a real field, computed rather than stored
• fnct: function to compute the field value (required)
def fnct(self, cr, uid, ids, field_name, arg, context)
returns a dictionary { ids→values } with values of type type
• fnct_inv: function used to write a value in the field instead
def fnct_inv(obj, cr, uid, id, name, value, fnct_inv_arg, context)
• type: type of simulated field (can be any other type except 'function')
• fnct_search: function used to search on this field
def fnct_search(obj, cr, uid, obj, name, args)
returns a list of tuples arguments for search(), e.g. [('id','in',[1,3,5])]
• obj: model _name of simulated field if it is a relational field
• store, multi: optimization mechanisms (see usage in Performance Section)
related(f1, f2, …, type='float', …) Shortcut field equivalent to browsing chained fields
• f1,f2,...: chained fields to reach target (f1 required) (→51)
• type: type of target field
property(obj, type='float', view_load=None, group_name=None, …)
Dynamic attribute with specific access rights
• obj: object (required)
• type: type of equivalent field
Tip: relational fields symmetry
• one2many ↔ many2one are symmetric
• many2many ↔ many2many are symmetric when inversed (swap field1 and field2 if explicit)
• one2many ↔ many2one + many2one ↔ one2many = many2many
Special / Reserved field names
A few field names are reserved for pre-defined behavior in OpenERP. Some of them
are created automatically by the system, and in that case any field wih that name will
be ignored.
id
unique system identifier for the object
name
field whose value is used to display the record in lists, etc.
if missing, set _rec_name to specify another field to use
active
toggle visibility: records with active set to False are hidden by default
sequence
defines order and allows drag&drop reordering if visible in list views
state
lifecycle stages for the object, used by the states attribute
parent_id
defines tree structure on records, and enables child_of operator
parent_left,
parent_right
used in conjunction with _parent_store flag on object, allows faster
access to tree structures (see also Performance Optimization section)
create_date,
create_uid,
write_date,
write_uid
used to log creator, last updater, date of creation and last update date of
the record. disabled if _log_access flag is set to False
(created by ORM, do not add them)
Working with the ORM
Inheriting from the osv.Model class makes all the ORM methods available on
business objects. These methods may be invoked on the self object within the
Python class itself (see examples in the table below), or from outside the class
by first obtaining an instance via the ORM pool system.
ORM usage sample
class
idea2
idea2(osv.Model):
_inherit = 'idea.idea'
def _score_calc(self,cr,uid,ids,field,arg,context=None):
res = {}
# This loop generates only 2 queries thanks to browse()!
for idea in self.browse(cr,uid,ids,context=context):
sum_vote = sum([v.vote for v in idea.vote_ids])
Copyright © 2013 Open Object Press - All rights reserved – See license on page 12.
p.3/12
72
73
74
75
76
77
78