7 .. _reference/orm/model:
12 .. - can't get autoattribute to import docstrings, so use regular attribute
15 .. currentmodule:: openerp.models
17 .. autoclass:: openerp.models.Model
19 .. rubric:: Structural attributes
23 business object name, in dot-notation (in module namespace)
25 .. attribute:: _rec_name
27 Alternative field to use as name, used by osv’s name_get()
30 .. attribute:: _inherit
32 * If :attr:`._name` is set, names of parent models to inherit from.
33 Can be a ``str`` if inheriting from a single parent
34 * If :attr:`._name` is unset, name of a single model to extend
37 See :ref:`reference/orm/inheritance`.
41 Ordering field when searching without an ordering specified (default:
48 Whether a database table should be created (default: ``True``)
50 If set to ``False``, override :meth:`.init` to create the database
55 Name of the table backing the model created when
56 :attr:`~openerp.models.Model._auto`, automatically generated by
59 .. attribute:: _inherits
61 dictionary mapping the _name of the parent business objects to the
62 names of the corresponding foreign key fields to use::
65 'a.model': 'a_field_id',
66 'b.model': 'b_field_id'
69 implements composition-based inheritance: the new model exposes all
70 the fields of the :attr:`~openerp.models.Model._inherits`-ed model but
71 stores none of them: the values themselves remain stored on the linked
76 if the same field is defined on multiple
77 :attr:`~openerp.models.Model._inherits`-ed
79 .. attribute:: _constraints
81 list of ``(constraint_function, message, fields)`` defining Python
82 constraints. The fields list is indicative
86 use :func:`~openerp.api.constrains`
88 .. attribute:: _sql_constraints
90 list of ``(name, sql_definition, message)`` triples defining SQL
91 constraints to execute when generating the backing table
93 .. attribute:: _parent_store
95 Alongside :attr:`~.parent_left` and :attr:`~.parent_right`, sets up a
96 `nested set <http://en.wikipedia.org/wiki/Nested_set_model>`_ to
97 enable fast hierarchical queries on the records of the current model
104 .. automethod:: create
105 .. automethod:: browse
106 .. automethod:: unlink
107 .. automethod:: write
113 .. automethod:: search
114 .. automethod:: search_count
115 .. automethod:: name_search
117 .. rubric:: Recordset operations
119 .. autoattribute:: ids
120 .. automethod:: ensure_one
121 .. automethod:: exists
122 .. automethod:: filtered
123 .. automethod:: sorted
124 .. automethod:: update
126 .. rubric:: Environment swapping
129 .. automethod:: with_context
130 .. automethod:: with_env
132 .. rubric:: Fields and views querying
134 .. automethod:: fields_get
135 .. automethod:: fields_view_get
139 .. automethod:: default_get
141 .. automethod:: name_get
142 .. automethod:: name_create
144 .. _reference/orm/model/automatic:
146 .. rubric:: Automatic fields
150 Identifier :class:`field <openerp.fields.Field>`
152 .. attribute:: _log_access
154 Whether log access fields (``create_date``, ``write_uid``, ...) should
155 be generated (default: ``True``)
157 .. attribute:: create_date
159 Date at which the record was created
161 :type: :class:`~openerp.field.Datetime`
163 .. attribute:: create_uid
165 Relational field to the user who created the record
169 .. attribute:: write_date
171 Date at which the record was last modified
173 :type: :class:`~openerp.field.Datetime`
175 .. attribute:: write_uid
177 Relational field to the last user who modified the record
181 .. rubric:: Reserved field names
183 A few field names are reserved for pre-defined behaviors beyond that of
184 automated fields. They should be defined on a model when the related
189 default value for :attr:`~._rec_name`, used to
190 display records in context where a representative "naming" is
193 :type: :class:`~openerp.fields.Char`
195 .. attribute:: active
197 toggles the global visibility of the record, if ``active`` is set to
198 ``False`` the record is invisible in most searches and listing
200 :type: :class:`~openerp.fields.Boolean`
202 .. attribute:: sequence
204 Alterable ordering criteria, allows drag-and-drop reordering of models
207 :type: :class:`~openerp.fields.Integer`
211 lifecycle stages of the object, used by the ``states`` attribute on
212 :class:`fields <openerp.fields.Field>`
214 :type: :class:`~openerp.fields.Selection`
216 .. attribute:: parent_id
218 used to order records in a tree structure and enables the ``child_of``
221 :type: :class:`~openerp.fields.Many2one`
223 .. attribute:: parent_left
225 used with :attr:`~._parent_store`, allows faster tree structure access
227 .. attribute:: parent_right
229 see :attr:`~.parent_left`
231 .. _reference/orm/decorators:
236 .. automodule:: openerp.api
237 :members: one, multi, model, depends, constrains, onchange, returns
239 .. _reference/orm/fields:
244 .. _reference/orm/fields/basic:
249 .. autodoc documents descriptors as attributes, even for the *definition* of
250 descriptors. As a result automodule:: openerp.fields lists all the field
251 classes as attributes without providing inheritance info or methods (though
252 we don't document methods as they're not useful for "external" devs)
253 (because we don't support pluggable field types) (or do we?)
255 .. autoclass:: openerp.fields.Field
257 .. autoclass:: openerp.fields.Char
260 .. autoclass:: openerp.fields.Boolean
263 .. autoclass:: openerp.fields.Integer
266 .. autoclass:: openerp.fields.Float
269 .. autoclass:: openerp.fields.Text
272 .. autoclass:: openerp.fields.Selection
275 .. autoclass:: openerp.fields.Html
278 .. autoclass:: openerp.fields.Date
280 :members: today, context_today, from_string, to_string
282 .. autoclass:: openerp.fields.Datetime
284 :members: now, context_timestamp, from_string, to_string
286 .. _reference/orm/fields/relational:
291 .. autoclass:: openerp.fields.Many2one
294 .. autoclass:: openerp.fields.One2many
297 .. autoclass:: openerp.fields.Many2many
300 .. autoclass:: openerp.fields.Reference
303 .. _reference/orm/inheritance:
305 Inheritance and extension
306 =========================
308 Odoo provides three different mechanisms to extend models in a modular way:
310 * creating a new model from an existing one, adding new information to the
311 copy but leaving the original module as-is
312 * extending models defined in other modules in-place, replacing the previous
314 * delegating some of the model's fields to records it contains
316 .. image:: ../images/inheritance_methods.png
319 Classical inheritance
320 ---------------------
322 When using the :attr:`~openerp.models.Model._inherit` and
323 :attr:`~openerp.models.Model._name` attributes together, Odoo creates a new
324 model using the existing one (provided via
325 :attr:`~openerp.models.Model._inherit`) as a base. The new model gets all the
326 fields, methods and meta-information (defaults & al) from its base.
328 .. literalinclude:: ../../openerp/addons/test_documentation_examples/inheritance.py
334 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_inheritance.py
340 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_inheritance.py
344 the second model has inherited from the first model's ``check`` method and its
345 ``name`` field, but overridden the ``call`` method, as when using standard
346 :ref:`Python inheritance <python:tut-inheritance>`.
351 When using :attr:`~openerp.models.Model._inherit` but leaving out
352 :attr:`~openerp.models.Model._name`, the new model replaces the existing one,
353 essentially extending it in-place. This is useful to add new fields or methods
354 to existing models (created in other modules), or to customize or reconfigure
355 them (e.g. to change their default sort order):
357 .. literalinclude:: ../../openerp/addons/test_documentation_examples/extension.py
361 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_extension.py
367 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_extension.py
371 .. note:: it will also yield the various :ref:`automatic fields
372 <reference/orm/model/automatic>` unless they've been disabled
377 The third inheritance mechanism provides more flexibility (it can be altered
378 at runtime) but less power: using the :attr:`~openerp.models.Model._inherits`
379 a model *delegates* the lookup of any field not found on the current model
380 to "children" models. The delegation is performed via
381 :class:`~openerp.fields.Reference` fields automatically set up on the parent
384 .. literalinclude:: ../../openerp/addons/test_documentation_examples/delegation.py
388 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_delegation.py
394 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_delegation.py
398 and it's possible to write directly on the delegated field:
400 .. literalinclude:: ../../openerp/addons/test_documentation_examples/tests/test_delegation.py
404 .. warning:: when using delegation inheritance, methods are *not* inherited,
407 .. _reference/orm/domains:
412 A domain is a list of criteria, each criterion being a triple (either a
413 ``list`` or a ``tuple``) of ``(field_name, operator, value)`` where:
415 ``field_name`` (``str``)
416 a field name of the current model, or a relationship traversal through
417 a :class:`~openerp.fields.Many2one` using dot-notation e.g. ``'street'``
418 or ``'partner_id.country'``
419 ``operator`` (``str``)
420 an operator used to compare the ``field_name`` with the ``value``. Valid
430 greater than or equal to
434 less than or equal to
436 unset or equals to (returns true if ``value`` is either ``None`` or
437 ``False``, otherwise behaves like ``=``)
439 matches ``field_name`` against the ``value`` pattern. An underscore
440 ``_`` in the pattern stands for (matches) any single character; a
441 percent sign ``%`` matches any string of zero or more characters.
443 matches ``field_name`` against the ``%value%`` pattern. Similar to
444 ``=like`` but wraps ``value`` with '%' before matching
446 doesn't match against the ``%value%`` pattern
448 case insensitive ``like``
450 case insensitive ``not like``
452 case insensitive ``=like``
454 is equal to any of the items from ``value``, ``value`` should be a
457 is unequal to all of the items from ``value``
459 is a child (descendant) of a ``value`` record.
461 Takes the semantics of the model into account (i.e following the
462 relationship field named by
463 :attr:`~openerp.models.Model._parent_name`).
466 variable type, must be comparable (through ``operator``) to the named
469 Domain criteria can be combined using logical operators in *prefix* form:
472 logical *AND*, default operation to combine criteria following one
473 another. Arity 2 (uses the next 2 criteria or combinations).
475 logical *OR*, arity 2.
477 logical *NOT*, arity 1.
479 .. tip:: Mostly to negate combinations of criteria
482 Individual criterion generally have a negative form (e.g. ``=`` ->
483 ``!=``, ``<`` -> ``>=``) which is simpler than negating the positive.
485 .. admonition:: Example
487 To search for partners named *ABC*, from belgium or germany, whose language
491 ('language.code','!=','en_US'),
492 '|',('country_id.code','=','be'),
493 ('country_id.code','=','de')]
495 This domain is interpreted as:
500 AND (language is NOT english)
501 AND (country is Belgium OR Germany)