ORM API - Odoo 11.0

Наборы записей

Добавлено в версии 8.0: This page documents the New API added in Odoo 8.0 which should be the primary development API going forward. It also provides information about porting from or bridging with the «old API» of versions 7 and earlier, but does not explicitly document that API. See the old documentation for that.

Interaction with models and records is performed through recordsets, a sorted set of records of the same model.

Методы, определенные для модели данных, выполняются над НЗ, а их self и представляет собой экземпляр НЗ:

class AModel(models.Model):
    _name = 'a.model'
    def a_method(self):
        # self can be anywhere between 0 records and all records in the
        # database
        self.do_operation()

Перебор НЗ приведет к созданию новых НЗ состоящих из одной записи (ОЗ), данный механизм можно сравнить с перебором строки Python который формирует строки из одного символа:

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), ...

Доступ к полям

Recordsets provide an «Active Record» interface: model fields can be read and written directly from the record as attributes, but only on singletons (single-record recordsets). 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

Trying to read or write a field on multiple records will raise an error.

Доступ к реляционному полю(Many2one, One2many, Many2many) всегда возвращает НЗ, пустой если поле не задано.

Запись кэша и предварительная выборка

Odoo поддерживает кэш для полей записей, так что не каждый доступ к полям вызывает запрос базы данных, что ужасно сказывалось бы на производительности. Следующий пример запрашивает базу данных только для первого выражения:

record.name             # first access reads value from database
record.name             # second access gets value from cache

Чтобы не считывать одно поле из одной записи за раз, Odoo выполняет предварительную выборку записей и полей, следуя некоторым эвристикам, чтобы получить хорошую производительность. Как только поле должно быть прочитано в данной записи, ORM фактически считывает это поле в большем НЗ и сохраняет возвращаемые значения в кэше для последующего использования. Унаследованный НЗ обычно представляет собой НЗ, из которого запись выбирается путем перебора. Более того, все простые хранимые поля (boolean, integer, float, char, text, date, datetime, selection, many2one) извлекаются целиком; Они соответствуют столбцам таблицы модели и эффективно извлекаются в одном запросе.

Рассмотрим следующий пример, где partners - НЗ из 1000 записей. Без предварительной выборки цикл будет выполнять 2000 запросов к базе данных. При предварительной выборке выполняется только один запрос:

for partner in partners:
    print partner.name          # first pass prefetches 'name' and 'lang'
                                # (and other fields) on all 'partners'
    print partner.lang

Предварительная выборка также работает на вторичных записях: при чтении значений реляционных полей (которые являются записями) подписываются на будущую предварительную выборку. При обращении к одной из этих вторичных записей происходит предварительная выборка всех вторичных записей из одной и той же модели данных. В следующем примере генерируются только два запроса: один для партнеров и один для стран:

countries = set()
for partner in partners:
    country = partner.country_id        # first pass prefetches all partners
    countries.add(country.name)         # first pass prefetches all countries

Set operations

Recordsets are immutable, but sets of the same model can be combined using various set operations, returning new recordsets. Set operations do not preserve order.

  • record in set возвращает запись (которая должна быть набором из 1 элемента) если таковая присутствует присутствует в НЗ set. record not in set - это обратная операция
  • set1 <= set2 и set1 < set2 возвращает НЗ set1 если он входит НЗ set2 (resp. strict)
  • set1 >= set2 and set1 > set2 возвращает НЗ set1 если он включает в себя НЗ set2 (resp. strict)
  • set1 | set2 возвращает объединение двух НЗ, новый НЗ содержит все записи, имеющиеся каждом источнике
  • set1 & set2 возвращает пересечение двух НЗ, новый НЗ содержит только те записи которые содержатся одновременно в обоих источниках
  • set1 - set2 возвращает новый НЗ, содержащий только записи НЗ set1, которые не входят в НЗ set2

Other recordset operations

НЗ это итерируемый объект, поэтому обычные инструменты Python доступны для работы с ним (map(), sorted(), ifilter(), …) однако они возвращают либо list или iterator, удаляя возможность вызывать методы для возвращенных результатов, или использовать операции НЗ.

Recordsets therefore provide these operations returning recordsets themselves (when possible):

filtered()

returns a recordset containing only records satisfying the provided predicate function. The predicate can also be a string to filter by a field being true or false:

# only keep records whose company is the current user's
records.filtered(lambda r: r.company_id == user.company_id)

# only keep records whose partner is a company
records.filtered("partner_id.is_company")
sorted()

returns a recordset sorted by the provided key function. If no key is provided, use the model’s default sort order:

# sort records by name
records.sorted(key=lambda r: r.name)
mapped()

applies the provided function to each record in the recordset, returns a recordset if the results are recordsets:

# returns a list of summing two fields for each record in the set
records.mapped(lambda r: r.field1 + r.field2)

The provided function can be a string to get field values:

# returns a list of names
records.mapped('name')

# returns a recordset of partners
record.mapped('partner_id')

# returns the union of all partner banks, with duplicates removed
record.mapped('partner_id.bank_ids')

Окружение (Environment)

Класс Environment хранит различные контекстные данные, используемые ORM: курсор базы данных (для формирования запросов к базе данных), текущий пользователь (для проверки прав доступа) и текущий контекст (хранение произвольных метаданных). В окружении также хранятся кэши.

All recordsets have an environment, which is immutable, can be accessed using env and gives access to the current user (user), the cursor (cr) or the context (context):

>>> records.env
<Environment object ...>
>>> records.env.user
res.user(3)
>>> records.env.cr
<Cursor object ...)

При создании НЗ из другого набора записей окружение наследуется. Окружение может быть использовано для получения пустого НЗ в другой модели данных а так же для формирования запроса к модели:

>>> self.env['res.partner']
res.partner
>>> self.env['res.partner'].search([['is_company', '=', True], ['customer', '=', True]])
res.partner(7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28, 74)

Изменение окружения

The environment can be customized from a recordset. This returns a new version of the recordset using the altered environment.

sudo()

creates a new environment with the provided user set, uses the administrator if none is provided (to bypass access rights/rules in safe contexts), returns a copy of the recordset it is called on using the new environment:

# create partner object as administrator
env['res.partner'].sudo().create({'name': "A Partner"})

# list partners visible by the "public" user
public = env.ref('base.public_user')
env['res.partner'].sudo(public).search([])
with_context()
  1. can take a single positional parameter, which replaces the current environment’s context
  2. can take any number of parameters by keyword, which are added to either the current environment’s context or the context set during step 1
# look for partner, or create one with specified timezone if none is
# found
env['res.partner'].with_context(tz=a_tz).find_or_create(email_address)
with_env()
replaces the existing environment entirely

Базовые методы ORM

search()

Takes a search domain, returns a recordset of matching records. Can return a subset of matching records (offset and limit parameters) and be ordered (order parameter):

>>> # searches the current model
>>> self.search([('is_company', '=', True), ('customer', '=', True)])
res.partner(7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28, 74)
>>> self.search([('is_company', '=', True)], limit=1).name
'Agrolait'
create()

Takes a number of field values, and returns a recordset containing the record created:

>>> self.create({'name': "New Name"})
res.partner(78)
write()

Takes a number of field values, writes them to all the records in its recordset. Does not return anything:

self.write({'name': "Newer Name"})
browse()

Takes a database id or a list of ids and returns a recordset, useful when record ids are obtained from outside Odoo (e.g. round-trip through external system) or when calling methods in the old API:

>>> self.browse([7, 18, 12])
res.partner(7, 18, 12)
exists()

Returns a new recordset containing only the records which exist in the database. Can be used to check whether a record (e.g. obtained externally) still exists:

if not record.exists():
    raise Exception("The record has been deleted")

or after calling a method which could have removed some records:

records.may_remove_some()
# only keep records which were not deleted
records = records.exists()
ref()

Environment method returning the record matching a provided external id:

>>> env.ref('base.group_public')
res.groups(2)
ensure_one()

checks that the recordset is a singleton (only contains a single record), raises an error otherwise:

records.ensure_one()
# is equivalent to but clearer than:
assert len(records) == 1, "Expected singleton"

Creating Models

Поля модели определяются как ее атрибуты:

from odoo import models, fields
class AModel(models.Model):
    _name = 'a.model.name'

    field1 = fields.Char()

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:

field2 = fields.Integer(string="an other field")

For the various field types and parameters, see the fields reference.

Default values are defined as parameters on fields, either a value:

a_field = fields.Char(default="a value")

or a function called to compute the default value, which should return that value:

def compute_default_value(self):
    return self.get_value()
a_field = fields.Char(default=compute_default_value)

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():

from odoo import api
total = fields.Float(compute='_compute_total')

@api.depends('value', 'tax')
def _compute_total(self):
    for record in self:
        record.total = record.value + record.value * record.tax
  • зависимости могут быть указанны в виде ссылки на поле, путь к которому вы описываете с помощью dot-нотации:

    @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
  • searching on a computed field can also be enabled by setting the search parameter. The value is a method name returning a Domains:

    upper_name = field.Char(compute='_compute_upper', search='_search_upper')
    
    def _search_upper(self, operator, value):
        if operator == 'like':
            operator = 'ilike'
        return [('name', operator, value)]
    
  • to allow setting values on a computed field, use the inverse parameter. It is the name of a function reversing the computation and setting the relevant fields:

    document = fields.Char(compute='_get_document', inverse='_set_document')
    
    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)
    
  • значения для нескольких полей могут быть вычислены одновременно одним и тем же методом, просто используйте его во всех полях и присвойте нужные значения для каждого из них:

    discount_value = fields.Float(compute='_apply_discount')
    total = fields.Float(compute='_apply_discount')
    
    @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
    

onchange: updating UI on the fly

When a user changes a field’s value in a form (but hasn’t saved the form yet), it can be useful to automatically update other fields based on that value e.g. updating a final total when the tax is changed or a new invoice line is added.

  • computed fields are automatically checked and recomputed, they do not need an onchange
  • for non-computed fields, the onchange() decorator is used to provide new field values:

    @api.onchange('field1', 'field2') # if these fields are changed, call method
    def check_change(self):
        if self.field1 < self.field2:
            self.field3 = True
    

    the changes performed during the method are then sent to the client program and become visible to the user

  • Both computed fields and new-API onchanges are automatically called by the client without having to add them in views
  • It is possible to suppress the trigger from a specific field by adding on_change="0" in a view:

    <field name="name" on_change="0"/>
    

    will not trigger any interface update when the field is edited by the user, even if there are function fields or explicit onchange depending on that field.

Low-level SQL

В окружении атрибут cr является курсором для выполнения транзакции в текущей базе данных и позволяет напрямую выполнять SQL запросы, также применяется для запросов, которые трудно выразить с помощью ORM (например, сложные join’ы), либо по причинам производительности:

self.env.cr.execute("some_sql", param1, param2, param3)

Поскольку модели данных используют один и тот же курсор, а класс Environment содержит различные кэши, эти кэшидолжны быть недействительными при изменении базы данных с помощью чистого SQL, или дальнейшее использование моделей может стать некогерентным. Необходимо очищать кэши при использовании CREATE, UPDATE или DELETE в SQL, но не SELECT (который просто читает базу данных).

Clearing caches can be performed using the invalidate_cache() method of the BaseModel object.

Compatibility between new API and old API

Odoo is currently transitioning from an older (less regular) API, it can be necessary to manually bridge from one to the other manually:

  • RPC layers (both XML-RPC and JSON-RPC) are expressed in terms of the old API, methods expressed purely in the new API are not available over RPC
  • overridable methods may be called from older pieces of code still written in the old API style

The big differences between the old and new APIs are:

  • values of the Environment (cursor, user id and context) are passed explicitly to methods instead
  • record data (ids) are passed explicitly to methods, and possibly not passed at all
  • methods tend to work on lists of ids instead of recordsets

By default, methods are assumed to use the new API style and are not callable from the old API style.

Two decorators can expose a new-style method to the old API:

model()

the method is exposed as not using ids, its recordset will generally be empty. Its «old API» signature is cr, uid, *arguments, context:

@api.model
def some_method(self, a_value):
    pass
# can be called as
old_style_model.some_method(cr, uid, a_value, context=context)
multi()

the method is exposed as taking a list of ids (possibly empty), its «old API» signature is cr, uid, ids, *arguments, context:

@api.multi
def some_method(self, a_value):
    pass
# can be called as
old_style_model.some_method(cr, uid, [id1, id2], a_value, context=context)

Because new-style APIs tend to return recordsets and old-style APIs tend to return lists of ids, there is also a decorator managing this:

returns()

the function is assumed to return a recordset, the first parameter should be the name of the recordset’s model or self (for the current model).

No effect if the method is called in new API style, but transforms the recordset into a list of ids when called from the old API style:

>>> @api.multi
... @api.returns('self')
... def some_method(self):
...     return self
>>> new_style_model = env['a.model'].browse(1, 2, 3)
>>> new_style_model.some_method()
a.model(1, 2, 3)
>>> old_style_model = pool['a.model']
>>> old_style_model.some_method(cr, uid, [1, 2, 3], context=context)
[1, 2, 3]

Model Reference

class odoo.models.Model(pool, cr)[исходный код]

Основной супер-класс для стандартных моделей Odoo, работающих с базой данных.

Модели Одоо создаются наследованием от этого класса:

class user(Model):
    ...

Позже система создаст экземпляр класса один раз для каждой базы данных (для которой установлен модуль, в котором объявлена модель).

Structural attributes

_name

business object name, in dot-notation (in module namespace)

_rec_name

Alternative field to use as name, used by osv’s name_get() (default: 'name')

_inherit
  • If _name is set, names of parent models to inherit from. Can be a str if inheriting from a single parent
  • Если атрибут _name не указан, то происходит расширение конкретной модели данных

See Наследование и расширение.

_order

Ordering field when searching without an ordering specified (default: 'id')

Type
_auto

Whether a database table should be created (default: True)

If set to False, override init() to create the database table

_table

Name of the table backing the model created when _auto, automatically generated by default.

_inherits

dictionary 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 _inherits-ed model but stores none of them: the values themselves remain stored on the linked record.

_constraints

list of (constraint_function, message, fields) defining Python constraints. The fields list is indicative

Не рекомендуется, начиная с версии 8.0: use constrains()

_sql_constraints

list of (name, sql_definition, message) triples defining SQL constraints to execute when generating the backing table

_parent_store

Alongside parent_left and parent_right, sets up a nested set to enable fast hierarchical queries on the records of the current model (default: False)

Type

CRUD

create(vals) → record[исходный код]

Creates a new record for the model.

The new record is initialized using the values from vals and if necessary those from default_get().

Параметры
vals (dict) –

values for the model’s fields, as a dictionary:

{'field_name': field_value, ...}

смотрите write() чтобы узнать детали

Результат
new record created
Исключение
  • AccessError
    • Если у пользователя нет прав на создание объекта
    • Если пользователь пытается обойти правила доступа для создания на запрошенном объекте
  • ValidateError – Если пользователь пытается ввести недопустимое значение для поля, которое не выбрано
  • UserError – Если в иерархии объектов будет создан цикл в результате операции (такой как установка объекта как собственного родителя)
browse([ids]) → records[исходный код]

Возвращает НЗ для id, предоставленных в качестве параметра в текущей среде.

Can take no ids, a single id or a sequence of ids.

Удаляет записи НЗ

Исключение
  • AccessError
    • Если у пользователя нет прав удаления на запрошенном объекте
    • Если пользователь пытается обойти правила доступа для удаления на запрошенном объекте
  • UserError – Если запись является свойством по умолчанию для других записей
write(vals)[исходный код]

Обновляет все записи в текущем НЗ с указанными значениями.

Параметры
vals (dict) – поля для обновления и их будущие значения например:: {'foo': 1, 'bar': "Qux"} установит значение поля foo в 1 а поля bar как "Qux" если такое допустимо (в противном случае будет вызвана ошибка).
Исключение
  • AccessError
    • Если у пользователя нет прав на запись для запрашиваемого объекта
    • Если пользователь пытается обойти правила доступа для записи на запрошенном объекте
  • ValidateError – Если пользователь пытается ввести недопустимое значение для поля, которое не выбрано
  • UserError – Если в иерархии объектов будет создан цикл в результате операции (такой как установка объекта как собственного родителя)
  • Для числовых полей (Integer, Float) значение должно быть соответствующего типа
  • Для Boolean, значение должно быть bool
  • Для Selection, значение должно соответствовать значениям выбора (обычно str, иногда int`)
  • Для Many2one, значение должно быть id записи
  • Другие нереляционные поля используют строку для значения

  • One2many и Many2many используют специальный формат «команд» для управления набором записей, хранящихся/связанных с этим полем.

    Этот формат представляет собой список триплетов, выполняемых последовательно, где каждый триплет является командой, выполняемой по набору записей. Не все команды применяются во всех ситуациях. Возможные команды:

    (0, _, values)
    Добавляет новую запись, созданную из предоставленного value.
    (1, id, values)
    Обновляет существующую запись c id id значениями в values. Нельзя использовать в create().
    (2, id, _)
    Удаляет запись с id id из НЗ, а затем удаляет его (из базы данных). Нельзя использовать в create().
    (3, id, _)
    removes the record of id id from the set, but does not delete it. Can not be used on One2many. Can not be used in create().
    (4, id, _)
    adds an existing record of id id to the set. Can not be used on One2many.
    (5, _, _)
    removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().
    (6, _, ids)
    Заменяет все существующие записи в НЗ списком ids, что эквивалентно использованию команды 5, за которой следует команда 4 для каждого id в ids.
read([fields])[исходный код]

Читает запрошенные поля для записей в self, низкоуровневом/RPC-методе. В коде Python предпочитайте browse().

Параметры
fields – список имен полей для возврата (по умолчанию все поля)
Результат
список словарей, отображающих имена полей с их значениями, с одним словарем на каждую запись
Исключение
AccessError – Если у пользователя нет прав на чтение некоторых из данных записей
read_group(domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True)[исходный код]

Get the list of records in list view grouped by the given groupby fields

Параметры
  • domain – list specifying search criteria [[„field_name“, „operator“, „value“], …]
  • fields (list) – list of fields present in the list view specified on the object
  • groupby (list) – Список с описаниями groupby , по которым записи будут сгруппированы. groupby описание представляет собой либо поле (тогда оно будет сгруппировано по этому полю), либо поле 'field: groupby_function' . Прямо сейчас единственными поддерживаемыми функциями являются 'day', 'week', 'month',``“quarter“`` или 'year' и они имеют смысл только для полей date/datetime.
  • offset (int) – необязательный параметр - количество записей для пропуска
  • limit (int) – необязательный параметр - максимальное количество возвращаемых записей
  • orderby (list) – необязательный параметр order by, для переопределения естественного порядка сортировки групп, см. search() (поддерживается только для many2one полей в настоящее время)
  • lazy (bool) – если true, результаты группируются только первым groupby , а остальные groupby помещаются в ключ __context. Если false, все groupby выполняются за один вызов.
Результат
список словарей (один словарь для каждой записи), содержащий: [UNKNOWN NODE problematic]значения полей, сгруппированных по полям в аргументе groupby * __domain: список кортежей, определяющих критерии поиска * __context: словарь с аргументом типа groupby
Тип результата
[{„field_name_1“: value, ..]
Исключение
AccessError
  • Если пользователь не имеет прав на запрашиваемый объект
  • Если пользователь пытается обойти правила доступа для чтения на запрошенном объекте

Searching

search(args[, offset=0][, limit=None][, order=None][, count=False])[исходный код]

Осуществляет поиск записей на базе args домен поиска.

Параметры
  • argsдомен поиска. Используйте пустой список для соответствия всем записям.
  • offset (int) – Количество игнорируемых результатов (по умолчанию: none)
  • limit (int) – Максимальное количество возвращаемых записей (по умолчанию: все)
  • order (str) – Строка сортировки
  • count (bool) – Если True, только подсчитывает и возвращает количество совпадающих записей (по умолчанию: False)
Результат
Не более [UNKNOWN NODE problematic]limit` записей, соответствующих критериям поиска
Исключение
AccessError
  • Если пользователь пытается обойти правила доступа для чтения на запрошенном объекте.
search_count(args) → int[исходный код]

Возвращает количество записей в текущей модели и совпадающих с доменом.

Ищет записи, у которых есть отображаемое имя, соответствующее заданному шаблону name, когда совпадает с указанным operator, а также соответствуют необязательной области поиска (args).

Это используется, например, для предоставления предложений, основанных на частичном значении для реляционного поля. Иногда можно рассматривать как обратную функцию name_get(), но это не гарантируется.

Этот метод эквивалентен вызову search() с поисковым доменом на основе display_name, а затем примененному name_get() к результату поиска.

Параметры
  • name (str) – Шаблон имени, с которым нужно сопоставить
  • args (list) – необязательный параметр - домен поиска (см. search() для синтаксиса), указав дальнейшие ограничения
  • operator (str) – оператор домена для сравнения с name, например 'like' или ' = '.
  • limit (int) – необязательный параметр - максимальное количество возвращаемых записей
Тип результата
list
Результат
Список пар (id, text_repr) для всех совпадающих записей.

Recordset operations

ids

List of actual record ids in this recordset (ignores placeholder ids for records to create)

ensure_one()[исходный код]

Verifies that the current recorset holds a single record. Raises an exception otherwise.

exists() → records[исходный код]

Возвращает подмножество записей в self, которые существуют, и помечает удаленные записи как таковые в кэше. Его можно использовать в качестве теста на записях

if record.exists():
    ...

По соглашению, новые записи возвращаются как существующие.

filtered(func)[исходный код]

Select the records in self such that func(rec) is true, and return them as a recordset.

Параметры
func – функция или разделенная точками последовательность имен полей
sorted(key=None, reverse=False)[исходный код]

Возвращает НЗ self, упорядоченный(отсортированный) по``key``.

Параметры
  • key – Либо функцию одного аргумента, которая возвращает ключ сравнения для каждой записи, либо имя поля, либо ` None``, в этом случае записи упорядочиваются в соответствии с порядком сортировки модели по умолчанию
  • reverse – Если True, возвращает результат в с сортировкой в обратном порядке
mapped(func)[исходный код]

Применяет func во ко всем записям в self, и возвращает результат в виде списка или НЗ (если func возвращает записи). В последнем случае порядок возвращаемого НЗ произволен.

Параметры
func – a function or a dot-separated sequence of field names (string); any falsy value simply returns the recordset self

Environment swapping

sudo([user=SUPERUSER])[исходный код]

Returns a new version of this recordset attached to the provided user.

By default this returns a SUPERUSER recordset, where access control and record rules are bypassed.

with_context([context][, **overrides]) → records[исходный код]

Возвращает новую версию этого набора записей, присоединенного к расширенному контексту.

Расширенный контекст является либо предоставленный context, в котором объеденены overrides, либо текущий контекст, в котором объединены overrides например:

# current context is {'key1': True}
r2 = records.with_context({}, key2=True)
# -> r2._context is {'key2': True}
r2 = records.with_context(key2=True)
# -> r2._context is {'key1': True, 'key2': True}
with_env(env)[исходный код]

Returns a new version of this recordset attached to the provided environment

Fields and views querying

fields_get([fields][, attributes])[исходный код]

Возвращает определение каждого поля.

The returned value is a dictionary (indiced by field name) of dictionaries. The _inherits’d fields are included. The string, help, and selection (if present) attributes are translated.

Параметры
  • allfields – список полей для документа, все, если они пусты или не указаны
  • attributes – список атрибутов описания, возвращаемых для каждого поля, все, если они пусты или не указаны
fields_view_get([view_id | view_type='form'])[исходный код]

Получить подробный состав запрашиваемого представления, например поля, модель, архитектуру представления

Параметры
  • view_id – id представления или None
  • view_type – Тип представления, возвращаемого, если view_id равно None („form“, „tree“, …)
  • toolbar – true для включения контекстных действий
  • submenu – deprecated
Результат
dictionary describing the composition of the requested view (including inherited views and extensions)
Исключение
  • AttributeError
    • Если унаследованное представление имеет неизвестную позицию для работы с другими, отличную от 'before', 'after', 'inside', 'replace',
    • Если в родительском представлении обнаружен какой-либо тег, отличный от 'position'
  • Invalid ArchitectureError – Если есть представление, отличное от form, tree, calendar, search и т. д., определенных в структуре

Miscellaneous methods

default_get(fields) → default_values[исходный код]

Возвращает значения по умолчанию для полей в fields_list. Значения по умолчанию определяются контекстом, пользовательскими настройками по умолчанию и самой моделью.

Параметры
fields_list – список имен полей
Результат
словарь, где ключ имя поля а значение - его значение по умолчанию, если оно есть.
copy(default=None)[исходный код]

Дублирует запись self, обновляя ее значениями по умолчанию

Параметры
default (dict) – Словарь значений поля для переопределения в исходных значениях скопированной записи, например: {'field_name': overridden_value, ...}
Результат
новая запись
name_get() → [(id, name), ...][исходный код]

Возвращает текстовое представление для записей в self. По умолчанию это значение поля display_name.

Результат
список пар (id, text_repr) для каждой записи
Тип результата
list(tuple)
name_create(name) → record[исходный код]

Создает новую запись, вызвав create(), указав только одно значение: отображаемое имя новой записи.

Новая запись будет инициализироваться с любыми значениями по умолчанию, применимыми к этой модели или предоставляемыми через контекст. Применяется обычное поведение create().

Параметры
name – Отображаемое имя создаваемой записи
Тип результата
tuple
Результат
name_get() значение созданной записи

Системные поля (Automatic fields)

id

ID field

_log_access

Whether log access fields (create_date, write_uid, …) should be generated (default: True)

create_date

Date at which the record was created

Type
Datetime
create_uid

Relational field to the user who created the record

Type
res.users
write_date

Date at which the record was last modified

Type
Datetime
write_uid

Relational field to the last user who modified the record

Type
res.users

Reserved field names

Несколько имен полей зарезервированы для предопределенных сценариев, которые отличаются от автоматически заполняемых полей. Они должны быть определены в модели данных, когда настраивается желаемое поведение:

name

default value for _rec_name, used to display records in context where a representative «naming» is necessary.

Type
имеет тип Char
active

toggles the global visibility of the record, if active is set to False the record is invisible in most searches and listing

Type
имеет тип Boolean
sequence

Alterable ordering criteria, allows drag-and-drop reordering of models in list views

state

lifecycle stages of the object, used by the states attribute on fields

Type
имеет тип Selection
parent_id

used to order records in a tree structure and enables the child_of operator in domains

Type
имеет тип Many2one
parent_left

used with _parent_store, allows faster tree structure access

parent_right

see parent_left

Декораторы методов

This module provides the elements for managing two different API styles, namely the «traditional» and «record» styles.

In the «traditional» style, parameters like the database cursor, user id, context dictionary and record ids (usually denoted as cr, uid, context, ids) are passed explicitly to all methods. In the «record» style, those parameters are hidden into model instances, which gives it a more object-oriented feel.

For instance, the statements:

model = self.pool.get(MODEL)
ids = model.search(cr, uid, DOMAIN, context=context)
for rec in model.browse(cr, uid, ids, context=context):
    print rec.name
model.write(cr, uid, ids, VALUES, context=context)

may also be written as:

env = Environment(cr, uid, context) # cr, uid, context wrapped in env
model = env[MODEL]                  # retrieve an instance of MODEL
recs = model.search(DOMAIN)         # search returns a recordset
for rec in recs:                    # iterate over the records
    print rec.name
recs.write(VALUES)                  # update all records in recs

Methods written in the «traditional» style are automatically decorated, following some heuristics based on parameter names.

odoo.api.multi(method)[исходный код]

Decorate a record-style method where self is a recordset. The method typically defines an operation on records. Such a method:

@api.multi
def method(self, args):
    ...

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)
recs.method(args)

model.method(cr, uid, ids, args, context=context)
odoo.api.model(method)[исходный код]

Задекорируйте метод в новом стиле, где self - НЗ, но его содержимое не релевантно, только модель данных. Таким способом:

@api.model
def method(self, args):
    ...

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)
recs.method(args)

model.method(cr, uid, args, context=context)

Notice that no ids are passed to the method in the traditional style.

odoo.api.depends(*args)[исходный код]

Возвращает декоратор, который задает зависимости полей для метода «compute» (для вычисляемых полей в новом стиле). Каждый аргумент должен быть строкой, состоящей из разделенных точками последовательностей имен полей:

pname = fields.Char(compute='_compute_pname')

@api.one
@api.depends('partner_id.name', 'partner_id.is_company')
def _compute_pname(self):
    if self.partner_id.is_company:
        self.pname = (self.partner_id.name or "").upper()
    else:
        self.pname = self.partner_id.name

В качестве аргумента можно передать одну функцию. В этом случае зависимости задаются вызовом функции внутри модели которой принадлежит поле.

odoo.api.constrains(*args)[исходный код]

Decorates a constraint checker. Each argument must be a field name used in the check:

@api.one
@api.constrains('name', 'description')
def _check_description(self):
    if self.name == self.description:
        raise ValidationError("Fields name and description must be different")

Вызывается для записей, если в одном из указанных полей было произведено изменение.

Should raise ValidationError if the validation failed.

odoo.api.onchange(*args)[исходный код]

Return a decorator to decorate an onchange method for given fields. Each argument must be a field name:

@api.onchange('partner_id')
def _onchange_partner(self):
    self.message = "Dear %s" % (self.partner_id.name or "")

В представлении в виде формы где появляется поле, метод вызывается, когда одно из заданных полей изменяется. Метод вызывается в псевдо-записи, содержащей значения, присутствующие в форме. Назначения полей в этой записи автоматически отправляются обратно клиенту.

The method may return a dictionary for changing field domains and pop up a warning message, like in the old API:

return {
    'domain': {'other_id': [('partner_id', '=', partner_id)]},
    'warning': {'title': "Warning", 'message': "What is this?"},
}
odoo.api.returns(model, downgrade=None, upgrade=None)[исходный код]

Возвращает декоратор для методов, возвращающих экземпляры model.

Параметры
  • model – наименование модели данных или 'self' для текущей модели данных
  • downgrade – функция downgrade(self, value, *args, **kwargs) для преобразования нового стиля value в старый
  • upgrade – функция upgrade(self, value, *args, **kwargs) для преобразования из старого стиля value в новый

Аргументы self, *args and **kwargs - это параметры, которые передаются методу в новом стиле.

Декоратор приспосабливает вывод метода к стилю API: id, ids или False для старого стиля и НЗ для нового:

@model
@returns('res.partner')
def find_partner(self, arg):
    ...     # return some record

# output depends on call style: traditional vs record style
partner_id = model.find_partner(cr, uid, arg, context=context)

# recs = model.browse(cr, uid, ids, context)
partner_record = recs.find_partner(arg)

Обратите внимание, что декорированный метод должен удовлетворять этому соглашению.

Эти декораторы автоматически наследуются: метод, который переопределяет декорированный существующий метод, будет задекорирован тем же @returns(model).

odoo.api.one(method)[исходный код]

Decorate a record-style method where self is expected to be a singleton instance. The decorated method automatically loops on records, and makes a list with the results. In case the method is decorated with returns(), it concatenates the resulting instances. Such a method:

@api.one
def method(self, args):
    return self.name

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)
names = recs.method(args)

names = model.method(cr, uid, ids, args, context=context)

Не рекомендуется, начиная с версии 9.0: one() often makes the code less clear and behaves in ways developers and readers may not expect.

It is strongly recommended to use multi() and either iterate on the self recordset or ensure that the recordset is a single record with ensure_one().

odoo.api.v7(method_v7)[исходный код]

Decorate a method that supports the old-style api only. A new-style api may be provided by redefining a method with the same name and decorated with v8():

@api.v7
def foo(self, cr, uid, ids, context=None):
    ...

@api.v8
def foo(self):
    ...

Special care must be taken if one method calls the other one, because the method may be overridden! In that case, one should call the method from the current class (say MyClass), for instance:

@api.v7
def foo(self, cr, uid, ids, context=None):
    # Beware: records.foo() may call an overriding of foo()
    records = self.browse(cr, uid, ids, context)
    return MyClass.foo(records)

Note that the wrapper method uses the docstring of the first method.

odoo.api.v8(method_v8)[исходный код]

Decorate a method that supports the new-style api only. An old-style api may be provided by redefining a method with the same name and decorated with v7():

@api.v8
def foo(self):
    ...

@api.v7
def foo(self, cr, uid, ids, context=None):
    ...

Note that the wrapper method uses the docstring of the first method.

Поля

Basic fields

class odoo.fields.Field(string=<object object>, **kwargs)[исходный код]

Дескриптор поля содержит определение поля и управляет доступом и присвоением соответствующего поля для записей. При инициализации поля могут быть предоставлены следующие атрибуты:

Параметры
  • string – the label of the field seen by users (string); if not set, the ORM takes the field name in the class (capitalized).
  • help – the tooltip of the field seen by users (string)
  • readonly – whether the field is readonly (boolean, by default False)
  • required – whether the value of the field is required (boolean, by default False)
  • index – whether the field is indexed in database (boolean, by default False)
  • default – Значение поля по умолчанию; может быть заранее определенным значением, либо быть результатом работы функции, которая принимает в качестве аргумента набор записей и возвращающая значение; используйте default=None, чтобы сбросить значения по умолчанию для поля
  • states – a dictionary mapping state values to lists of UI attribute-value pairs; possible attributes are: „readonly“, „required“, „invisible“. Note: 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 – список разделенных запятыми xml-идентификаторов (строка); ограничивает доступ к полям только для пользователей указанных групп
  • copy (bool) – определяет следует ли копировать значение поля при дублировании записи (по умолчанию: True для обычных полей, False для полей типа one2many и вычисляемых полей, включая поля свойств и реляционные поля)
  • oldname (string) – the previous name of this field, so that ORM can rename it automatically at migration

Computed fields

One can define a field whose value is computed instead of simply being read from the database. The attributes that are specific to computed fields are given below. To define such a field, simply provide a value for the attribute compute.

Параметры
  • compute – Имя метода, который вычисляет значение поля
  • inverse – имя метода, который делает обратные вычисления (необязательный атрибут)
  • search – имя метода, реализующего поиск по полю (необязательный атрибут)
  • store – whether the field is stored in database (boolean, by default False on computed fields)
  • compute_sudo – whether the field should be recomputed as superuser to bypass access rights (boolean, by default False) Note that this has no effects on non-stored computed fields

The methods given for compute, inverse and search are model methods. Their signature is shown in the following example:

upper = fields.Char(compute='_compute_upper',
                    inverse='_inverse_upper',
                    search='_search_upper')

@api.depends('name')
def _compute_upper(self):
    for rec in self:
        rec.upper = rec.name.upper() if rec.name else False

def _inverse_upper(self):
    for rec in self:
        rec.name = rec.upper.lower() if rec.upper else False

def _search_upper(self, operator, value):
    if operator == 'like':
        operator = 'ilike'
    return [('name', operator, value)]

The compute method has to assign the field on all records of the invoked recordset. The decorator odoo.api.depends() must be applied on the compute method to specify the field dependencies; those dependencies are used to determine when to recompute the field; recomputation is automatic and guarantees cache/database consistency. Note that the same method can be used for several fields, you simply have to assign all the given fields in the method; the method will be invoked once for all those fields.

By default, a computed field is not stored to the database, and is computed on-the-fly. Adding the attribute store=True will store the field’s values in the database. The advantage of a stored field is that searching on that field is done by the database itself. The disadvantage is that it requires database updates when the field must be recomputed.

The inverse method, as its name says, does the inverse of the compute method: the invoked records have a value for the field, and you must apply the necessary changes on the field dependencies such that the computation gives the expected value. Note that a computed field without an inverse method is readonly by default.

Метод поиска search при обработке доменов перед выполнением фактического поиска по модели. Он должен возвращать домен, эквивалентный условию: field operator value.

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 attribute

Параметры
related – последовательность имен полей

Некоторые атрибуты полей автоматически копируются из исходного поля, если они не переопределены: строка, help, readonly, required (только если все поля в последовательности обязательны) , groups, digits, size, translate, sanitize, selection, comodel_name, domain, context. Все семантически-свободные атрибуты копируются из исходного поля.

По умолчанию значения связанных полей не сохраняются в базе данных. Добавьте атрибут store = True, чтобы он сохранялся, как и вычисляемые поля. Связанные поля автоматически пересчитываются при изменении их зависимостей.

Company-dependent fields

Formerly known as „property“ fields, the value of those fields depends on the company. In other words, users that belong to different companies may see different values for the field on a given record.

Параметры
company_dependent – whether the field is company-dependent (boolean)

Incremental definition

A field is defined as class attribute on a model class. If the model is extended (see Model), one can also extend the field definition by redefining a field with the same name and same type on the subclass. In that case, the attributes of the field are taken from the parent class and overridden by the ones given in subclasses.

Например, второй класс ниже только добавляет всплывающую подсказку в поле 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")
class odoo.fields.Char(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._String

Базовое строковое поле может быть ограничено по длине, обычно отображается в виде одной строки на стороне клиента.

Параметры
  • size (int) – максимальный размер значений, хранящихся для этого поля
  • translate – включает перевод значений поля; используйте translate = True для перевода значений полей в целом; translate также может быть функцией имеющий следующий вид translate(callback, value) где осуществляет перевод value используя callback(term), чтобы получить перевод терминов.
class odoo.fields.Boolean(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field
class odoo.fields.Integer(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field
class odoo.fields.Float(string=<object object>, digits=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field

The precision digits are given by the attribute

Параметры
digits – a pair (total, decimal), or a function taking a database cursor and returning a pair (total, decimal)
class odoo.fields.Text(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._String

Very similar to Char but used for longer contents, does not have a size and usually displayed as a multiline text box.

Параметры
translate – включает перевод значений поля; используйте translate = True для перевода значений полей в целом; translate также может быть функцией имеющий следующий вид translate(callback, value) где осуществляет перевод value используя callback(term), чтобы получить перевод терминов.
class odoo.fields.Selection(selection=<object object>, string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field
Параметры
  • selection – specifies the possible values for this field. It is given as either a list of pairs (value, string), or a model method, or a method name.
  • selection_add – provides an extension of the selection in the case of an overridden field. It is a list of pairs (value, string).

The attribute selection is mandatory except in the case of related fields or field extensions.

class odoo.fields.Html(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._String
class odoo.fields.Date(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field
static context_today(record, timestamp=None)[исходный код]

Return the current date as seen in the client’s timezone in a format fit for date fields. This method may be used to compute default values.

Параметры
timestamp (datetime) – optional datetime value to use instead of the current date and time (must be a datetime, regular dates can’t be converted between timezones.)
Тип результата
static from_string(value)[исходный код]

Convert an ORM value into a date value.

static to_string(value)[исходный код]

Convert a date value into the format expected by the ORM.

static today(*args)[исходный код]

Return the current day in the format expected by the ORM. This function may be used to compute default values.

class odoo.fields.Datetime(string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Field
static context_timestamp(record, timestamp)[исходный код]

Returns the given timestamp converted to the client’s timezone. 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 fields.datetime.now() should be used instead.

Параметры
timestamp (datetime) – naive datetime value (expressed in UTC) to be converted to the client timezone
Тип результата
datetime
Результат
timestamp converted to timezone-aware datetime in context timezone
static from_string(value)[исходный код]

Преобразует ORM value в значение datetime.

static now(*args)[исходный код]

Return the current day and time in the format expected by the ORM. This function may be used to compute default values.

static to_string(value)[исходный код]

Convert a datetime value into the format expected by the ORM.

Реляционные поля

class odoo.fields.Many2one(comodel_name=<object object>, string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._Relational

Значение такого поля - это набор записей размера 0 (нет записи) или 1 (одна запись).

Параметры
  • comodel_name – name of the target model (string)
  • domain – необязательный параметр - домен для установки значений кандидатов на стороне клиента (домен или строка)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • ondelete – определяет что делать, если запись, на которую ссылается удалена; возможные значения: 'set null', 'restrict', 'cascade'
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate – установите его в True, чтобы сделать поля целевой модели доступными из текущей модели (соответствует _inherits)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

class odoo.fields.One2many(comodel_name=<object object>, inverse_name=<object object>, string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._RelationalMulti

Поле One2many; значение такого поля является набором записей всех записей в comodel_name, так таким образом поле inverse_name было равно текущей записи.

Параметры
  • comodel_name – name of the target model (string)
  • inverse_name – name of the inverse Many2one field in comodel_name (string)
  • domain – необязательный параметр - домен для установки значений кандидатов на стороне клиента (домен или строка)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • auto_join – whether JOINs are generated upon search through that field (boolean, by default False)
  • limit – optional limit to use upon read (integer)

Атрибуты comodel_name и inverse_name обязательны, за исключением реляционных полей или расширений для полей.

class odoo.fields.Many2many(comodel_name=<object object>, relation=<object object>, column1=<object object>, column2=<object object>, string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields._RelationalMulti

Поле Many2many; Значением такого поля является набор записей.

Параметры
comodel_name – name of the target model (string)

The attribute comodel_name is mandatory except in the case of related fields or field extensions.

Параметры
  • relation – optional name of the table that stores the relation in the database (string)
  • column1 – optional name of the column referring to «these» records in the table relation (string)
  • column2 – optional name of the column referring to «those» records in the table relation (string)

Атрибуты relation, column1 и column2 являются необязательными. Если они не указаны, имена автоматически генерируются из имен моделей, если именя model_name и comodel_name отличаются!

Параметры
  • domain – необязательный параметр - домен для установки значений кандидатов на стороне клиента (домен или строка)
  • context – an optional context to use on the client side when handling that field (dictionary)
  • limit – optional limit to use upon read (integer)
class odoo.fields.Reference(selection=<object object>, string=<object object>, **kwargs)[исходный код]
Базовые классы: odoo.fields.Selection

Наследование и расширение

Odoo обеспечивает три различных механизма для расширения моделей данных по модульному принципу:

  • создание новой модели данных из уже существующей, добавляя новую информацию в копию, но оставляя исходный модуль как есть
  • расширение моделей , определенных в других модулях, заменяя предыдущую версию
  • делегировании некоторых полей моделей к записям, которые они содержат

Классическое наследование

При использовании атрибутов _inherit и _name вместе, Odoo создает новую модель, используя уже существующую (с помощью атрибута _inherit) в качестве базиса. Новая модель получает все поля, методы и мета-информацию (defaults & al) от базисной модели.


class Inheritance0(models.Model):
    _name = 'inheritance.0'

    name = fields.Char()

    def call(self):
        return self.check("model 0")

    def check(self, s):
        return "This is {} record {}".format(s, self.name)

class Inheritance1(models.Model):
    _name = 'inheritance.1'
    _inherit = 'inheritance.0'

    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()

даст следующий результат:

            "This is model 0 record A"
            "This is model 1 record B"

вторая модель унаследовала от первой метод проверки check и ее поле name, но переопределила метод call, как при использовании стандартного механизма наследования Python.

Расширение

При использовании _inherit, но не учитывая _name, новая модель данных заменит собой уже существующую, расширяя ее. Это полезно для добавления новых полей или методов в существующие модели данных (созданные в других модулях), или их индивидуальной настройки (например, чтобы изменить их порядок сортировки по умолчанию):

    _name = 'extension.0'

    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]

даст следующий результат:

        {'name': "A", 'description': "Extended"}

Делегирование

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:


class Child0(models.Model):
    _name = 'delegation.child0'

    field_0 = fields.Integer()

class Child1(models.Model):
    _name = 'delegation.child1'

    field_1 = fields.Integer()

class Delegating(models.Model):
    _name = 'delegation.parent'

    _inherits = {
        'delegation.child0': 'child0_id',
        'delegation.child1': 'child1_id',
    }

    child0_id = fields.Many2one('delegation.child0', required=True, ondelete='cascade')
    child1_id = fields.Many2one('delegation.child1', required=True, ondelete='cascade')
        record = env['delegation.parent'].create({
            'child0_id': env['delegation.child0'].create({'field_0': 0}).id,
            'child1_id': env['delegation.child1'].create({'field_1': 1}).id,
        })
            record.field_0
            record.field_1

даст результат:

            0
            1

есть возможность сделать запись прямо в делегированное поле:

        record.write({'field_1': 4})

Domains

Домен - это список критериев, каждый критерий состоит из трех частей ( является либо списком или кортежем) и состоит из (имя_поля, оператор, значние) где:

имя_поля (str)
имя поля текущей модели или отношение через класс Many2one с использованием dot-нотации, например 'street' или 'partner_id.country'
operator (str)

оператор, используемый для сравнения имя_поля со значением. Допустимые операторы:

=
равно
!=
не равно
>
больше чем
>=
больше чем или равно
<
меньше чем
<=
меньше чем или равно
=?
не задано или равно (возварщает true если значение является либо``None`` или False, в противном случае ведет себя как =)
=like
сопоставляет имя_поля с шаблоном значения. Подчеркивание _ в шаблоне означает (соответствует) любому одиночному символу; знак процента % соответствует любой строке из нуля или более символов.
like
сопоставляет имя_поля с шаблоном %значения%. Так же как =like, но оборачивает значение в % перед сравнением
not like
не сопоставляет с шаблоном %value%
ilike
не чувтсвительный к регистру like
not ilike
не чувствительный к регистру not like
=ilike
не чувтсвительный к регистру =like
in
равно любому из элементов значения, значение должно быть списком элементов
not in
не равно любому из элементов значения
child_of

is a child (descendant) of a value record.

Принимает во внимание семантику модели данных (т. е. за следующим реляционным полем , с наименованием _parent_name).

value
variable type, must be comparable (through operator) to the named field

Критерии домена могут быть объединены с помощью логических операторов в виде prefix:

'&'
логическое И, операция по умолчанию для объединения критериев, следующих друг за другом. Арность 2 (использует следующие 2 критерия или комбинации).
'|'
логическое ИЛИ, арность 2.
'!'

Логическое НЕ, арность 1.

Porting from the old API to the new API

  • bare lists of ids are to be avoided in the new API, use recordsets instead
  • methods still written in the old API should be automatically bridged by the ORM, no need to switch to the old API, just call them as if they were a new API method. See Automatic bridging of old API methods for more details.
  • search() returns a recordset, no point in e.g. browsing its result
  • fields.related and fields.function are replaced by using a normal field type with either a related= or a compute= parameter
  • depends() on compute= methods must be complete, it must list all the fields and sub-fields which the compute method uses. It is better to have too many dependencies (will recompute the field in cases where that is not needed) than not enough (will forget to recompute the field and then values will be incorrect)
  • remove all onchange methods on computed fields. Computed fields are automatically re-computed when one of their dependencies is changed, and that is used to auto-generate onchange by the client
  • the decorators model() and multi() are for bridging when calling from the old API context, for internal or pure new-api (e.g. compute) they are useless
  • remove _default, replace by default= parameter on corresponding fields
  • if a field’s string= is the titlecased version of the field name:

    name = fields.Char(string="Name")
    

    it is useless and should be removed

  • the multi= parameter does not do anything on new API fields use the same compute= methods on all relevant fields for the same result
  • provide compute=, inverse= and search= methods by name (as a string), this makes them overridable (removes the need for an intermediate «trampoline» function)
  • double check that all fields and methods have different names, there is no warning in case of collision (because Python handles it before Odoo sees anything)
  • the normal new-api import is from odoo import fields, models. If compatibility decorators are necessary, use from odoo import api, fields, models
  • avoid the one() decorator, it probably does not do what you expect
  • remove explicit definition of create_uid, create_date, write_uid and write_date fields: they are now created as regular «legitimate» fields, and can be read and written like any other field out-of-the-box
  • when straight conversion is impossible (semantics can not be bridged) or the «old API» version is not desirable and could be improved for the new API, it is possible to use completely different «old API» and «new API» implementations for the same method name using v7() and v8(). The method should first be defined using the old-API style and decorated with v7(), it should then be re-defined using the exact same name but the new-API style and decorated with v8(). Calls from an old-API context will be dispatched to the first implementation and calls from a new-API context will be dispatched to the second implementation. One implementation can call (and frequently does) call the other by switching context.

  • uses of _columns or _all_columns should be replaced by _fields, which provides access to instances of new-style odoo.fields.Field instances (rather than old-style odoo.osv.fields._column).

    Non-stored computed fields created using the new API style are not available in _columns and can only be inspected through _fields

  • reassigning self in a method is probably unnecessary and may break translation introspection
  • Environment objects rely on some threadlocal state, which has to be set up before using them. It is necessary to do so using the odoo.api.Environment.manage() context manager when trying to use the new API in contexts where it hasn’t been set up yet, such as new threads or a Python interactive environment:

    >>> from odoo import api, modules
    >>> r = modules.registry.RegistryManager.get('test')
    >>> cr = r.cursor()
    >>> env = api.Environment(cr, 1, {})
    Traceback (most recent call last):
      ...
    AttributeError: environments
    >>> with api.Environment.manage():
    ...     env = api.Environment(cr, 1, {})
    ...     print env['res.partner'].browse(1)
    ...
    res.partner(1,)
    

Automatic bridging of old API methods

When models are initialized, all methods are automatically scanned and bridged if they look like models declared in the old API style. This bridging makes them transparently callable from new-API-style methods.

Methods are matched as «old-API style» if their second positional parameter (after self) is called either cr or cursor. The system also recognizes the third positional parameter being called uid or user and the fourth being called id or ids. It also recognizes the presence of any parameter called context.

When calling such methods from a new API context, the system will automatically fill matched parameters from the current Environment (for cr, user and context) or the current recordset (for id and ids).

In the rare cases where it is necessary, the bridging can be customized by decorating the old-style method:

  • disabling it entirely, by decorating a method with noguess() there will be no bridging and methods will be called the exact same way from the new and old API styles
  • defining the bridge explicitly, this is mostly for methods which are matched incorrectly (because parameters are named in unexpected ways):

    cr()
    will automatically prepend the current cursor to explicitly provided parameters, positionally
    cr_uid()
    will automatically prepend the current cursor and user’s id to explictly provided parameters
    cr_uid_ids()
    will automatically prepend the current cursor, user’s id and recordset’s ids to explicitly provided parameters
    cr_uid_id()

    will loop over the current recordset and call the method once for each record, prepending the current cursor, user’s id and record’s id to explicitly provided parameters.

    All of these methods have a _context-suffixed version (e.g. cr_uid_context()) which also passes the current context by keyword.

  • dual implementations using v7() and v8() will be ignored as they provide their own «bridging»