ORM API

Object Relational Mapping module:
  • Hierarchical structure

  • Constraints consistency and validation

  • Object metadata depends on its status

  • Optimised processing by complex query (multiple actions at once)

  • Default field values

  • Permissions optimisation

  • Persistent object: DB postgresql

  • Data conversion

  • Multi-level caching system

  • Two different inheritance mechanisms

  • Rich set of field types:
    • classical (varchar, integer, boolean, …)

    • relational (one2many, many2one, many2many)

    • functional

型号

模型字段定义为模型本身的属性:

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

    field1 = fields.Char()

警告

这意味着您不能定义一个与方法同名的字段,否则后者将会悄悄地覆盖前者。

默认情况下,字段的标签(用户可见名称)是字段名称的大写版本,可以使用 string 参数覆盖。

field2 = fields.Integer(string="Field Label")

对于字段类型和参数的列表,请参见 字段参考

默认值是作为字段参数定义的,可以是一个值:

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

或者作为一个被调用以计算默认值的函数,该函数应该返回该值:

def _default_name(self):
    return self.get_value()

name = fields.Char(default=lambda self: self._default_name())

API

class odoo.models.BaseModel[源代码]

Base class for Odoo models.

Odoo models are created by inheriting one of the following:

  • Model for regular database-persisted models

  • TransientModel for temporary data, stored in the database but automatically vacuumed every so often

  • AbstractModel for abstract super classes meant to be shared by multiple inheriting models

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.

Every model instance is a “recordset”, i.e., an ordered collection of records of the model. Recordsets are returned by methods like browse(), search(), or field accesses. Records have no explicit representation: a record is represented as a recordset of one record.

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.

Automatically defaults to True for Model and TransientModel, False for AbstractModel.

小技巧

To create a model without any table, inherit from AbstractModel.

_log_access

Whether the ORM should automatically generate and update the 访问日志字段.

Defaults to whatever value was set for _auto.

_table = None

SQL table name used by model if _auto

_sql_constraints: list[tuple[str, str, str]] = []

SQL constraints [(name, sql_def, message)]

_register = False

registry visibility

_abstract = True

Whether the model is abstract.

另请参阅

AbstractModel

_transient = False

Whether the model is transient.

另请参阅

TransientModel

_name: str | None = None

the model name (in dot-notation, module namespace)

_description: str | None = None

the model’s informal name

_inherit: str | list[str] | tuple[str, ...] = ()

Python-inherited models:

类型

str or list(str) or tuple(str)

注解

  • 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.

警告

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.

Alongside a parent_path field, sets up an indexed storage of the tree structure of records, to enable faster hierarchical queries on the records of the current model using the child_of and parent_of domain operators.

_fold_name = 'fold'

field to determine folded groups in kanban views

抽象模型

odoo.models.AbstractModel[源代码]

alias of odoo.models.BaseModel

模型

class odoo.models.Model(env: api.Environment, ids: tuple[IdType, ...], prefetch_ids: Reversible[IdType])[源代码]

Main super-class for regular database-persisted Odoo models.

Odoo models are created by inheriting from this class:

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.

Automatically defaults to True for Model and TransientModel, False for AbstractModel.

小技巧

To create a model without any table, inherit from AbstractModel.

_abstract = False

Whether the model is abstract.

另请参阅

AbstractModel

瞬态模型

class odoo.models.TransientModel(env: api.Environment, ids: tuple[IdType, ...], prefetch_ids: Reversible[IdType])[源代码]

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.

_transient_max_count = 0

maximum number of transient records, unlimited if 0

_transient_max_hours = 1.0

maximum idle lifetime (in hours), unlimited if 0

_transient_vacuum()[源代码]

Clean the transient records.

This unlinks old records from the transient model tables whenever the _transient_max_count or _transient_max_hours conditions (if any) are reached.

Actual cleaning will happen only once every 5 minutes. This means this method can be called frequently (e.g. whenever a new record is created).

Example with both max_hours and max_count active:

Suppose max_hours = 0.2 (aka 12 minutes), max_count = 20, there are 55 rows in the table, 10 created/changed in the last 5 minutes, an additional 12 created/changed between 5 and 10 minutes ago, the rest created/changed more than 12 minutes ago.

  • age based vacuum will leave the 22 rows created/changed in the last 12 minutes

  • count based vacuum will wipe out another 12 rows. Not just 2, otherwise each addition would immediately cause the maximum to be reached again.

  • the 10 rows that have been created/changed the last 5 minutes will NOT be deleted

字段

class odoo.fields.Field[源代码]

The field descriptor contains the field definition, and manages accesses and assignments of the corresponding field on records. The following attributes may be provided when instantiating a field:

参数
  • string (str) – the label of the field seen by users; if not set, the ORM takes the field name in the class (capitalized).

  • help (str) – the tooltip of the field seen by users

  • readonly (bool) –

    whether the field is readonly (default: False)

    This only has an impact on the UI. Any field assignation in code will work (if the field is a stored field or an inversable one).

  • required (bool) – whether the value of the field is required (default: False)

  • index (str) –

    whether the field is indexed in database, and the kind of index. Note: this has no effect on non-stored and virtual fields. The possible values are:

    • "btree" or True: standard index, good for many2one

    • "btree_not_null": BTREE index without NULL values (useful when most

      values are NULL, or when NULL is never searched for)

    • "trigram": Generalized Inverted Index (GIN) with trigrams (good for full-text search)

    • None or False: no index (default)

  • 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

  • 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 is stored on the model table as jsonb dict with the company id as the key.

    The field’s default values stored in model ir.default are used as fallbacks for unspecified values in the jsonb dict.

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

  • aggregator (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

    • count : number of rows

    • count_distinct : number of distinct rows

    • bool_and : true if all values are true, otherwise false

    • bool_or : true if at least one value is true, otherwise false

    • max : maximum value of all values

    • min : minimum value of all values

    • avg : the average (arithmetic mean) of all values

    • sum : sum of all values

  • group_expand (str) –

    function used to expand read_group results when grouping on the current field. For selection fields, group_expand=True automatically expands groups for all selection keys.

    @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])
    

计算字段

参数
  • compute (str) –

    name of a method that computes the field

  • precompute (bool) –

    whether the field should be computed before record insertion in database. Should be used to specify manually some fields as precompute=True when the field can be computed before record insertion. (e.g. avoid statistics fields based on search/read_group), many2one linking to the previous record, … (default: False)

    警告

    Precomputation only happens when no explicit value and no default value is provided to create(). This means that a default value disables the precomputation, even if the field is specified as precompute=True.

    Precomputing a field can be counterproductive if the records of the given model are not created in batch. Consider the situation were many records are created one by one. If the field is not precomputed, it will normally be computed in batch at the flush(), and the prefetching mechanism will help making the computation efficient. On the other hand, if the field is precomputed, the computation will be made one by one, and will therefore not be able to take advantage of the prefetching mechanism.

    Following the remark above, precomputed fields can be interesting on the lines of a one2many, which are usually created in batch by the ORM itself, provided that they are created by writing on the record that contains them.

  • compute_sudo (bool) – whether the field should be recomputed as superuser to bypass access rights (by default True for stored fields, False for non stored fields)

  • recursive (bool) – whether the field has recursive dependencies (the field X has a dependency like parent_id.X); declaring a field recursive must be explicit to guarantee that recomputation is correct

  • inverse (str) – name of a method that inverses the field (optional)

  • search (str) – name of a method that implement search on the field (optional)

  • related (str) – sequence of field names

  • default_export_compatible (bool) –

    whether the field must be exported by default in an import-compatible export

基本字段

class odoo.fields.Boolean[源代码]

Encapsulates a bool.

class odoo.fields.Char[源代码]

Basic string field, can be length-limited, usually displayed as a single-line string in clients.

参数
  • 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.

class odoo.fields.Float[源代码]

Encapsulates a float.

The precision digits are given by the (optional) digits attribute.

参数

digits (tuple(int,int) or str) – a pair (total, decimal) or a string referencing a DecimalPrecision record name.

When a float is a quantity associated with an unit of measure, it is important to use the right tool to compare or round values with the correct precision.

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.

示例

To round a quantity with the precision of the unit of measure:

fields.Float.round(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)

To check if the quantity is zero with the precision of the unit of measure:

fields.Float.is_zero(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)

To compare two quantities:

field.Float.compare(self.product_uom_qty, self.qty_done, precision_rounding=self.product_uom_id.rounding)

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

class odoo.fields.Integer[源代码]

Encapsulates an int.

高级字段

class odoo.fields.Binary[源代码]

Encapsulates a binary content (e.g. a file).

参数

attachment (bool) – whether the field should be stored as ir_attachment or in a column of the model’s table (default: True).

class odoo.fields.Html[源代码]

Encapsulates an html code content.

参数
  • sanitize (bool) – whether value must be sanitized (default: True)

  • sanitize_overridable (bool) – whether the sanitation can be bypassed by the users part of the base.group_sanitize_override group (default: False)

  • sanitize_tags (bool) – whether to sanitize tags (only a white list of attributes is accepted, default: True)

  • sanitize_attributes (bool) – whether to sanitize attributes (only a white list of attributes is accepted, default: True)

  • sanitize_style (bool) – whether to sanitize style attributes (default: False)

  • sanitize_conditional_comments (bool) – whether to kill conditional comments. (default: True)

  • sanitize_output_method (bool) – whether to sanitize using html or xhtml (default: html)

  • strip_style (bool) – whether to strip style attributes (removed and therefore not sanitized, default: False)

  • strip_classes (bool) – whether to strip classes attributes (default: False)

class odoo.fields.Image[源代码]

Encapsulates an image, extending Binary.

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.

参数
  • 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)

  • verify_resolution (bool) – whether the image resolution should be verified to ensure it doesn’t go over the maximum image resolution (default: True). See odoo.tools.image.ImageProcess for maximum image resolution (default: 50e6).

注解

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.

class odoo.fields.Monetary[源代码]

Encapsulates a float expressed in a given res_currency.

The decimal precision and currency symbol are taken from the currency_field attribute.

参数

currency_field (str) – name of the Many2one field holding the res_currency this monetary field is expressed in (default: 'currency_id')

class odoo.fields.Selection[源代码]

Encapsulates an exclusive choice between different values.

参数
  • selection (list(tuple(str,str)) or callable or str) – specifies the possible values for this field. It is given as either a list of pairs (value, label), or a model method, or a method name.

  • selection_add (list(tuple(str,str))) –

    provides an extension of the selection in the case of an overridden field. It is a list of pairs (value, label) or singletons (value,), where singleton values must appear in the overridden selection. The new values are inserted in an order that is consistent with the overridden selection and this list:

    selection = [('a', 'A'), ('b', 'B')]
    selection_add = [('c', 'C'), ('b',)]
    > result = [('a', 'A'), ('c', 'C'), ('b', 'B')]
    

  • 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

    • ’set VALUE’ – all records with this option will be set to the given value

    • <callable> – a callable whose first and only argument will be the set of records containing the specified Selection option, for custom processing

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

class odoo.fields.Text[源代码]

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

参数

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.

日期(时间)字段

DatesDatetimes 是任何类型的商业应用程序中非常重要的字段。它们的错误使用可能会导致看不见但痛苦的错误,本节旨在为Odoo开发人员提供避免错误使用这些字段所需的知识。

当给日期/时间字段赋值时,以下选项是有效的:

  • 一个 datedatetime 对象。

  • 一个符合服务器格式的字符串:

    • YYYY-MM-DD for Date fields,

    • YYYY-MM-DD HH:MM:SS for Datetime fields.

  • FalseNone.

Date和Datetime字段类有助手方法,尝试转换为兼容类型:

Example

解析来自外部来源的日期/日期时间::

fields.Date.to_date(self._context.get('date_from'))

日期/时间比较最佳实践:

  • 日期字段 只能 与日期对象进行比较。

  • Datetime字段 只能 与datetime对象进行比较。

警告

表示日期和日期时间的字符串可以相互比较,但结果可能不是预期的结果,因为日期时间字符串始终大于日期字符串,因此 强烈 不建议这种做法。

日期和日期时间的常见操作,如加法、减法或获取一个周期的开始/结束,都可以通过 DateDatetime 进行操作。这些辅助函数也可以通过导入 odoo.tools.date_utils 来使用。

注解

时区

日期时间字段在数据库中以 timestamp without timezone 列的形式存储,并以UTC时区存储。这是有意设计的,因为它使Odoo数据库独立于托管服务器系统的时区。时区转换完全由客户端管理。

class odoo.fields.Date[源代码]

Encapsulates a python date object.

static start_of(value: odoo.tools.date_utils.D, granularity: Literal['year', 'quarter', 'month', 'week', 'day', 'hour']) odoo.tools.date_utils.D[源代码]

Get start of a time period from a date or a datetime.

参数
  • value – initial date or datetime.

  • granularity – type of period in string, can be year, quarter, month, week, day or hour.

返回

a date/datetime object corresponding to the start of the specified period.

static end_of(value: odoo.tools.date_utils.D, granularity: Literal['year', 'quarter', 'month', 'week', 'day', 'hour']) odoo.tools.date_utils.D[源代码]

Get end of a time period from a date or a datetime.

参数
  • value – initial date or datetime.

  • granularity – Type of period in string, can be year, quarter, month, week, day or hour.

返回

A date/datetime object corresponding to the start of the specified period.

static add(value: odoo.tools.date_utils.D, *args, **kwargs) odoo.tools.date_utils.D[源代码]

Return the sum of value and a relativedelta.

参数
  • value – initial date or datetime.

  • args – positional args to pass directly to relativedelta.

  • kwargs – keyword args to pass directly to relativedelta.

返回

the resulting date/datetime.

static subtract(value: odoo.tools.date_utils.D, *args, **kwargs) odoo.tools.date_utils.D[源代码]

Return the difference between value and a relativedelta.

参数
  • value – initial date or datetime.

  • args – positional args to pass directly to relativedelta.

  • kwargs – keyword args to pass directly to relativedelta.

返回

the resulting date/datetime.

static today(*args)[源代码]

Return the current day in the format expected by the ORM.

注解

This function may be used to compute default values.

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.

参数
  • record – recordset from which the timezone will be obtained.

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

返回类型

date

static to_date(value)[源代码]

Attempt to convert value to a date object.

警告

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

参数

value (str or date or datetime) – value to convert.

返回

an object representing value.

返回类型

date or None

static to_string(value)[源代码]

Convert a date or datetime object to a string.

参数

value – value to convert.

返回

a string representing value in the server’s date format, if value is of type datetime, the hours, minute, seconds, tzinfo will be truncated.

返回类型

str

class odoo.fields.Datetime[源代码]

Encapsulates a python datetime object.

static start_of(value: odoo.tools.date_utils.D, granularity: Literal['year', 'quarter', 'month', 'week', 'day', 'hour']) odoo.tools.date_utils.D[源代码]

Get start of a time period from a date or a datetime.

参数
  • value – initial date or datetime.

  • granularity – type of period in string, can be year, quarter, month, week, day or hour.

返回

a date/datetime object corresponding to the start of the specified period.

static end_of(value: odoo.tools.date_utils.D, granularity: Literal['year', 'quarter', 'month', 'week', 'day', 'hour']) odoo.tools.date_utils.D[源代码]

Get end of a time period from a date or a datetime.

参数
  • value – initial date or datetime.

  • granularity – Type of period in string, can be year, quarter, month, week, day or hour.

返回

A date/datetime object corresponding to the start of the specified period.

static add(value: odoo.tools.date_utils.D, *args, **kwargs) odoo.tools.date_utils.D[源代码]

Return the sum of value and a relativedelta.

参数
  • value – initial date or datetime.

  • args – positional args to pass directly to relativedelta.

  • kwargs – keyword args to pass directly to relativedelta.

返回

the resulting date/datetime.

static subtract(value: odoo.tools.date_utils.D, *args, **kwargs) odoo.tools.date_utils.D[源代码]

Return the difference between value and a relativedelta.

参数
  • value – initial date or datetime.

  • args – positional args to pass directly to relativedelta.

  • kwargs – keyword args to pass directly to relativedelta.

返回

the resulting date/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 today(*args)[源代码]

Return the current day, at midnight (00:00:00).

static context_timestamp(record, timestamp)[源代码]

Return 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, now() should be used instead.

参数
  • record – recordset from which the timezone will be obtained.

  • timestamp (datetime) – naive datetime value (expressed in UTC) to be converted to the client timezone.

返回

timestamp converted to timezone-aware datetime in context timezone.

返回类型

datetime

static to_datetime(value)[源代码]

Convert an ORM value into a datetime value.

参数

value (str or date or datetime) – value to convert.

返回

an object representing value.

返回类型

datetime or None

static to_string(value)[源代码]

Convert a datetime or date object to a string.

参数

value (datetime or date) – value to convert.

返回

a string representing value in the server’s datetime format, if value is of type date, the time portion will be midnight (00:00:00).

返回类型

str

关系型字段

class odoo.fields.Many2one[源代码]

The value of such a field is a recordset of size 0 (no record) or 1 (a single record).

参数
  • 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 a python expression that will be evaluated to provide domain)

  • context (dict) – an optional context to use on the client side when handling that field

  • ondelete (str) – what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'

  • auto_join (bool) – whether JOINs are generated upon search through that field (default: False)

  • delegate (bool) – set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

  • check_company (bool) – Mark the field to be verified in _check_company(). Has a different behaviour depending on whether the field is company_dependent or not. Constrains non-company-dependent fields to target records whose company_id(s) are compatible with the record’s company_id(s). Constrains company_dependent fields to target records whose company_id(s) are compatible with the currently active company.

class odoo.fields.One2many[源代码]

One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.

参数
  • comodel_name (str) – name of the target model

  • inverse_name (str) – name of the inverse Many2one field in comodel_name

  • domain – an optional domain to set on candidate values on the client side (domain or a python expression that will be evaluated to provide domain)

  • context (dict) – an optional context to use on the client side when handling that field

  • auto_join (bool) – whether JOINs are generated upon search through that field (default: False)

The attributes comodel_name and inverse_name are mandatory except in the case of related fields or field extensions.

class odoo.fields.Many2many[源代码]

Many2many field; the value of such a field is the recordset.

参数
  • 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.

参数
  • domain – an optional domain to set on candidate values on the client side (domain or a python expression that will be evaluated to provide domain)

  • 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.

class odoo.fields.Command[源代码]

One2many and Many2many fields expect a special command to manipulate the relation they implement.

Internally, each command is a 3-elements tuple where the first element is a mandatory integer that identifies the command, the second element is either the related record id to apply the command on (commands update, delete, unlink and link) either 0 (commands create, clear and set), the third element is either the values to write on the record (commands create and update) either the new ids list of related records (command set), either 0 (commands delete, unlink, link, and clear).

Via Python, we encourage developers craft new commands via the various functions of this namespace. We also encourage developers to use the command identifier constant names when comparing the 1st element of existing commands.

Via RPC, it is impossible nor to use the functions nor the command constant names. It is required to instead write the literal 3-elements tuple where the first element is the integer identifier of the command.

CREATE = 0
UPDATE = 1
DELETE = 2
CLEAR = 5
SET = 6
classmethod create(values: dict)[源代码]

Create new records in the comodel using values, link the created records to self.

In case of a Many2many relation, one unique new record is created in the comodel such that all records in self are linked to the new record.

In case of a One2many relation, one new record is created in the comodel for every record in self such that every record in self is linked to exactly one of the new records.

Return the command triple (CREATE, 0, values)

classmethod update(id: int, values: dict)[源代码]

Write values on the related record.

Return the command triple (UPDATE, id, values)

classmethod delete(id: int)[源代码]

Remove the related record from the database and remove its relation with self.

In case of a Many2many relation, removing the record from the database may be prevented if it is still linked to other records.

Return the command triple (DELETE, id, 0)

Remove the relation between self and the related record.

In case of a One2many relation, the given record is deleted from the database if the inverse field is set as ondelete='cascade'. Otherwise, the value of the inverse field is set to False and the record is kept.

Return the command triple (UNLINK, id, 0)

Add a relation between self and the related record.

Return the command triple (LINK, id, 0)

classmethod clear()[源代码]

Remove all records from the relation with self. It behaves like executing the unlink command on every record.

Return the command triple (CLEAR, 0, 0)

classmethod set(ids: list)[源代码]

Replace the current relations of self by the given ones. It behaves like executing the unlink command on every removed relation then executing the link command on every new relation.

Return the command triple (SET, 0, ids)

伪关系字段

class odoo.fields.Reference[源代码]

Pseudo-relational field (no FK in database).

The field value is stored as a string following the pattern "res_model,res_id" in database.

class odoo.fields.Many2oneReference[源代码]

Pseudo-relational field (no FK in database).

The field value is stored as an integer id 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.

参数

model_field (str) – name of the Char where the model name is stored.

计算字段

字段可以通过使用 compute 参数进行计算(而不是直接从数据库中读取)。它必须将计算值分配给字段。如果它使用其他 字段 的值,则应使用 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
  • 使用子字段时,依赖项可以是点分路径:

    @api.depends('line_ids.value')
    def _compute_total(self):
        for record in self:
            record.total = sum(line.value for line in record.line_ids)
    
  • 计算字段默认不会被存储,只有在请求时才会计算并返回。将 store=True 设置为真将会把它们存储在数据库中并自动启用搜索。

  • 计算字段的搜索也可以通过设置 search 参数来启用。该值是一个返回 搜索域 的方法名。:

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

    搜索方法在执行实际搜索之前,在处理域时被调用。它必须返回一个等价于条件 field operator value 的域。

  • 默认情况下,计算字段是只读的。要允许在计算字段上 设置 值,请使用 inverse 参数。它是一个函数名称,用于反转计算并设置相关字段:

    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')
    
    @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
    

警告

虽然可以在多个字段中使用相同的计算方法,但不建议在反向方法中这样做。

在计算反向关系时, 所有 使用该反向关系的字段都受到保护,这意味着即使它们的值不在缓存中,也无法计算它们的值。

如果访问这些字段并且它们的值不在缓存中,ORM 将简单地为这些字段返回默认值 False 。这意味着除了触发反向方法的字段之外的反向字段的值可能不会给出它们的正确值,这可能会破坏反向方法的预期行为。

自动字段

Model.id

标识符 field

如果当前记录集的长度为1,则返回其中唯一记录的ID。

否则抛出错误。

Model.display_name

Name field 默认在网页客户端中显示的字段

默认情况下,它等于 _rec_name 值字段,但可以通过重写 _compute_display_name 来自定义行为

访问日志字段

这些字段在启用 _log_access 时会自动设置和更新。可以禁用它以避免在不需要它们的表上创建或更新这些字段。

默认情况下, _log_access 的值与 _auto 相同

Model.create_date

记录创建时间, Datetime

Model.create_uid

存储创建记录的 whoMany2oneres.users

Model.write_date

记录上次更新的时间, Datetime

Model.write_uid

记录最后更新的人, Many2oneres.users

警告

_log_access 必须TransientModel 上启用。

保留字段名称

一些字段名称被保留用于预定义的行为,超出了自动化字段的范畴。当需要相关的行为时,应在模型上定义它们:

Model.name

默认值为 _rec_name,用于在需要代表性“命名”的上下文中显示记录。

Char

Model.active

切换记录的全局可见性,如果 active 设置为 False ,则该记录在大多数搜索和列表中都是不可见的。

Boolean

特殊方法:

toggle_active()[源代码]

Inverses the value of active on the records in self.

Model.action_archive()[源代码]

Sets active to False on a recordset, by calling toggle_active() on its currently active records.

Model.action_unarchive()[源代码]

Sets active to True on a recordset, by calling toggle_active() on its currently inactive records.

Model.state

对象的生命周期阶段,由 fields 上的 states 属性使用。

Selection

Model.parent_id

default_value of _parent_name, 用于组织树形结构中的记录,并在域中启用 child_ofparent_of 运算符。

Many2one

Model.parent_path

_parent_store 设置为 True 时,用于存储反映 _parent_name 的树结构的值,并优化搜索域中的 child_ofparent_of 运算符。必须使用 index=True 声明以确保正常运行。

Char

Model.company_id

Odoo 多公司行为所使用的主字段名称。

用于 :meth:~odoo.models._check_company 检查多公司一致性。定义记录是否在公司之间共享(没有值)或仅可由给定公司的用户访问。

Many2one :type: res_company

记录集

与模型和记录的交互是通过记录集进行的,记录集是相同模型的记录的有序集合。

警告

与其名称所暗示的相反,目前记录集中可能包含重复项。这在未来可能会改变。

在模型上定义的方法在记录集上执行,它们的 self 是一个记录集:

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

迭代记录集将产生新的 单个记录 集合(”单例”),就像在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), ...

字段访问

记录集提供了一个“Active Record”接口:模型字段可以直接作为属性从记录中读取和写入。

注解

当访问可能包含多个记录的记录集上的非关系字段时,请使用 mapped():

total_qty = sum(self.mapped('qty'))

字段值也可以像字典项一样访问,这比使用动态字段名的 getattr() 更加优雅和安全。设置字段的值会触发对数据库的更新:

>>> record.name
Example Name
>>> record.company_id.name
Company Name
>>> record.name = "Bob"
>>> field = "name"
>>> record[field]
Bob

警告

尝试读取多个记录上的字段将会对非关系型字段引发错误。

访问关系字段 (Many2one, One2many, Many2many) 总是 返回一个记录集,如果字段未设置则为空。

记录缓存和预取

Odoo 维护记录字段的缓存,以便不是每个字段访问都会发出数据库请求,这对性能来说是可怕的。以下示例仅查询第一个语句的数据库:

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

为了避免逐个读取每个记录的每个字段,Odoo 预取 记录和字段,遵循一些启发式规则以获得良好的性能。一旦必须在给定记录上读取一个字段,ORM 实际上会在更大的记录集上读取该字段,并将返回的值存储在缓存中以供以后使用。预取的记录集通常是通过迭代从中获取记录的记录集。此外,所有简单存储的字段(布尔值、整数、浮点数、字符、文本、日期、日期时间、选择、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

另请参阅

方法 search_fetch()fetch() 可用于填充记录的缓存,通常在预取机制效果不佳的情况下使用。

方法修饰器

The Odoo API module defines Odoo Environments and method decorators.

odoo.api.model(method: odoo.api.T) odoo.api.T[源代码]

Decorate a record-style method where self is a recordset, but its contents is not relevant, only the model is. Such a method:

@api.model
def method(self, args):
    ...
odoo.api.constrains(*args: str) Callable[[T], T][源代码]

Decorate a constraint checker.

Each argument must be a field name used in the check:

@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.

Should raise ValidationError if the validation failed.

警告

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

One may also pass a single function as argument. In that case, the field names are given by calling the function with a model instance.

odoo.api.depends(*args: str) Callable[[T], T][源代码]

Return a decorator that specifies the field dependencies of a “compute” method (for new-style function fields). Each argument must be a string that consists in a dot-separated sequence of field names:

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.

odoo.api.onchange(*args)[源代码]

Return a decorator to decorate an onchange method for given fields.

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.

Each argument must be a field name:

@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': 'notification'},
}

If the type is set to notification, the warning will be displayed in a notification. Otherwise it will be displayed in a dialog as default.

警告

@onchange only supports simple field names, dotted names (fields of relational fields e.g. partner_id.tz) are not supported and will be ignored

危险

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.

警告

It is not possible for a one2many or many2many field to modify itself via onchange. This is a webclient limitation - see #2693.

odoo.api.returns(model, downgrade=None, upgrade=None)[源代码]

Return a decorator for methods that return instances of model.

参数
  • model – a model name, or 'self' for the current model

  • downgrade – a function downgrade(self, value, *args, **kwargs) to convert the record-style value to a traditional-style output

  • upgrade – a function upgrade(self, value, *args, **kwargs) to convert the traditional-style value to a record-style output

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

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

Note that the decorated method must satisfy that convention.

Those decorators are automatically inherited: a method that overrides a decorated existing method will be decorated with the same @returns(model).

odoo.api.autovacuum(method)[源代码]

Decorate a method so that it is called by the daily vacuum cron job (model ir.autovacuum). This is typically used for garbage-collection-like tasks that do not deserve a specific cron job.

odoo.api.depends_context(*args)[源代码]

Return a decorator that specifies the context dependencies of a non-stored “compute” method. Each argument is a key in the context’s dictionary:

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['pricelist'])
        else:
            pricelist = self.env['product.pricelist'].get_default_pricelist()
        product.price = pricelist._get_products_price(product).get(product.id, 0.0)

All dependencies must be hashable. The following keys have special support:

  • company (value in context or current company id),

  • uid (current user id and superuser flag),

  • active_test (value in env.context or value in field.context).

odoo.api.model_create_multi(method: odoo.api.T) odoo.api.T[源代码]

Decorate a method that takes a list of dictionaries and creates multiple records. The method may be called with either a single dict or a list of dicts:

record = model.create(vals)
records = model.create([vals, ...])
odoo.api.ondelete(*, at_uninstall)[源代码]

Mark a method to be executed during unlink().

The goal of this decorator is to allow client-side errors when unlinking records if, from a business point of view, it does not make sense to delete such records. For instance, a user should not be able to delete a validated sales order.

While this could be implemented by simply overriding the method unlink on the model, it has the drawback of not being compatible with module uninstallation. When uninstalling the module, the override could raise user errors, but we shouldn’t care because the module is being uninstalled, and thus all records related to the module should be removed anyway.

This means that by overriding unlink, there is a big chance that some tables/records may remain as leftover data from the uninstalled module. This leaves the database in an inconsistent state. Moreover, there is a risk of conflicts if the module is ever reinstalled on that database.

Methods decorated with @ondelete should raise an error following some conditions, and by convention, the method should be named either _unlink_if_<condition> or _unlink_except_<not_condition>.

@api.ondelete(at_uninstall=False)
def _unlink_if_user_inactive(self):
    if any(user.active for user in self):
        raise UserError("Can't delete an active user!")

# same as above but with _unlink_except_* as method name
@api.ondelete(at_uninstall=False)
def _unlink_except_active_user(self):
    if any(user.active for user in self):
        raise UserError("Can't delete an active user!")
参数

at_uninstall (bool) – Whether the decorated method should be called if the module that implements said method is being uninstalled. Should almost always be False, so that module uninstallation does not trigger those errors.

危险

The parameter at_uninstall should only be set to True if the check you are implementing also applies when uninstalling the module.

For instance, it doesn’t matter if when uninstalling sale, validated sales orders are being deleted because all data pertaining to sale should be deleted anyway, in that case at_uninstall should be set to False.

However, it makes sense to prevent the removal of the default language if no other languages are installed, since deleting the default language will break a lot of basic behavior. In this case, at_uninstall should be set to True.

环境

class odoo.api.Environment(cr, uid, context, su=False, uid_origin=None)[源代码]

The environment stores various contextual data used by the ORM:

  • cr: the current database cursor (for database queries);

  • uid: the current user id (for access rights checks);

  • context: the current context dictionary (arbitrary metadata);

  • su: whether in superuser mode.

It provides access to the registry by implementing a mapping from model names to models. It also holds a cache for records, and a data structure to manage recomputations.

>>> records.env
<Environment object ...>
>>> records.env.uid
3
>>> 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)

一些延迟加载的属性可用于访问环境(上下文)数据:

Environment.lang

Return the current language code.

返回类型

str

Environment.user

Return the current user (as an instance).

返回

current user - sudoed

返回类型

res.users record

Environment.company

Return the current company (as an instance).

If not specified in the context (allowed_company_ids), fallback on current user main company.

引发

AccessError – invalid or unauthorized allowed_company_ids context key content.

返回

current company (default=`self.user.company_id`), with the current environment

返回类型

res.company record

警告

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.

If not specified in the context(allowed_company_ids), fallback on current user companies.

引发

AccessError – invalid or unauthorized allowed_company_ids context key content.

返回

current companies (default=`self.user.company_ids`), with the current environment

返回类型

res.company recordset

警告

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.ref(xml_id, raise_if_not_found=True)[源代码]

Return the record corresponding to the given xml_id.

参数
  • xml_id (str) – record xml_id, under the format <module.id>

  • raise_if_not_found (bool) – whether the method should raise if record is not found

返回

Found record or None

引发

ValueError – if record wasn’t found and raise_if_not_found is True

Environment.is_superuser()[源代码]

Return whether the environment is in superuser mode.

Environment.is_admin()[源代码]

Return whether the current user has group “Access Rights”, or is in superuser mode.

Environment.is_system()[源代码]

Return whether the current user has group “Settings”, or is in superuser mode.

Environment.execute_query(query: odoo.tools.sql.SQL) list[tuple][源代码]

Execute the given query, fetch its result and it as a list of tuples (or an empty list if no result to fetch). The method automatically flushes all the fields in the metadata of the query.

修改环境

Model.with_context([context][, **overrides]) Model[源代码]

Returns a new version of this recordset attached to an extended context.

The extended context is either the provided context in which overrides are merged or the current context in which overrides are merged e.g.:

# 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}
Model.with_user(user)[源代码]

Return a new version of this recordset attached to the given user, in non-superuser mode, unless user is the superuser (by convention, the superuser is always in superuser mode.)

Model.with_company(company)[源代码]

Return a new version of this recordset with a modified context, such that:

result.env.company = company
result.env.companies = self.env.companies | company
参数

company (res_company or int) – main company of the new environment.

警告

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.

Model.with_env(env: api.Environment) Self[源代码]

Return a new version of this recordset attached to the provided environment.

参数

env (Environment) –

注解

The returned recordset has the same prefetch object as self.

Model.sudo([flag=True])[源代码]

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.

警告

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.

注解

The returned recordset has the same prefetch object as self.

SQL 执行

在环境中, cr 属性是当前数据库事务的游标,允许直接执行 SQL,无论是为了难以使用 ORM 表达的查询(例如复杂的连接)还是出于性能原因:

self.env.cr.execute("some_sql", params)

警告

执行原始 SQL 会绕过 ORM,从而绕过 Odoo 的安全规则。请确保在使用用户输入时对查询进行过滤,并且如果您不真正需要使用 SQL 查询,则优先使用 ORM 工具。

构建 SQL 查询的推荐方式是使用包装器对象

class odoo.tools.SQL(code: str | SQL = '', /, *args, to_flush: Field | None = None, **kwargs)[源代码]

An object that wraps SQL code with its parameters, like:

sql = SQL("UPDATE TABLE foo SET a = %s, b = %s", 'hello', 42)
cr.execute(sql)

The code is given as a %-format string, and supports either positional arguments (with %s) or named arguments (with %(name)s). Escaped characters (like "%%") are not supported, though. The arguments are meant to be merged into the code using the % formatting operator.

The SQL wrapper is designed to be composable: the arguments can be either actual parameters, or SQL objects themselves:

sql = SQL(
    "UPDATE TABLE %s SET %s",
    SQL.identifier(tablename),
    SQL("%s = %s", SQL.identifier(columnname), value),
)

The combined SQL code is given by sql.code, while the corresponding combined parameters are given by the list sql.params. This allows to combine any number of SQL terms without having to separately combine their parameters, which can be tedious, bug-prone, and is the main downside of psycopg2.sql <https://www.psycopg.org/docs/sql.html>.

The second purpose of the wrapper is to discourage SQL injections. Indeed, if code is a string literal (not a dynamic string), then the SQL object made with code is guaranteed to be safe, provided the SQL objects within its parameters are themselves safe.

The wrapper may also contain some metadata to_flush. If not None, its value is a field which the SQL code depends on. The metadata of a wrapper and its parts can be accessed by the iterator sql.to_flush.

join(args: Iterable) SQL[源代码]

Join SQL objects or parameters with self as a separator.

classmethod identifier(name: str, subname: str | None = None, to_flush: Field | None = None) SQL[源代码]

Return an SQL object that represents an identifier.

关于模型,需要知道的一件重要的事情是它们不一定立即执行数据库更新。为了性能原因,框架会延迟在修改记录后重新计算字段的操作。而且一些数据库更新也会被延迟。因此,在查询数据库之前,必须确保它包含了查询所需的相关数据。这个操作被称为 flushing ,它执行了预期的数据库更新。

Example

# make sure that 'partner_id' is up-to-date in database
self.env['model'].flush_model(['partner_id'])

self.env.cr.execute(SQL("SELECT id FROM model WHERE partner_id IN %s", ids))
ids = [row[0] for row in self.env.cr.fetchall()]

在执行每个 SQL 查询之前,必须刷新该查询所需的数据。刷新有三个级别,每个级别都有自己的 API。可以刷新所有内容、模型的所有记录或某些特定记录。由于延迟更新通常可以提高性能,因此建议在刷新时要 具体

Environment.flush_all()[源代码]

Flush all pending computations and updates to the database.

Model.flush_model(fnames=None)[源代码]

Process the pending computations and database updates on self’s model. When the parameter is given, the method guarantees that at least the given fields are flushed to the database. More fields can be flushed, though.

参数

fnames – optional iterable of field names to flush

Model.flush_recordset(fnames=None)[源代码]

Process the pending computations and database updates on the records self. When the parameter is given, the method guarantees that at least the given fields on records self are flushed to the database. More fields and records can be flushed, though.

参数

fnames – optional iterable of field names to flush

因为模型使用相同的游标,并且 Environment 持有各种缓存,所以在使用原始 SQL 修改 数据库时,必须使这些缓存失效,否则模型的进一步使用可能会变得不一致。在使用 SQL 中的 CREATEUPDATEDELETE 时,必须清除缓存,但 SELECT 不需要(因为它只是读取数据库)。

Example

# make sure 'state' is up-to-date in database
self.env['model'].flush_model(['state'])

self.env.cr.execute("UPDATE model SET state=%s WHERE state=%s", ['new', 'old'])

# invalidate 'state' from the cache
self.env['model'].invalidate_model(['state'])

与刷新类似,可以使整个缓存失效,使模型的所有记录的缓存失效,或使特定记录的缓存失效。甚至可以使某些记录的特定字段或模型的所有记录的特定字段失效。由于缓存通常可以提高性能,我们建议在使缓存失效时要 具体

Environment.invalidate_all(flush=True)[源代码]

Invalidate the cache of all records.

参数

flush – whether pending updates should be flushed before invalidation. It is True by default, which ensures cache consistency. Do not use this parameter unless you know what you are doing.

Model.invalidate_model(fnames=None, flush=True)[源代码]

Invalidate the cache of all records of self’s model, when the cached values no longer correspond to the database values. If the parameter is given, only the given fields are invalidated from cache.

参数
  • fnames – optional iterable of field names to invalidate

  • flush – whether pending updates should be flushed before invalidation. It is True by default, which ensures cache consistency. Do not use this parameter unless you know what you are doing.

Model.invalidate_recordset(fnames=None, flush=True)[源代码]

Invalidate the cache of the records in self, when the cached values no longer correspond to the database values. If the parameter is given, only the given fields on self are invalidated from cache.

参数
  • fnames – optional iterable of field names to invalidate

  • flush – whether pending updates should be flushed before invalidation. It is True by default, which ensures cache consistency. Do not use this parameter unless you know what you are doing.

上述方法可以使缓存和数据库保持一致。但是,如果计算字段的依赖关系在数据库中被修改,就必须通知模型重新计算计算字段。框架需要知道的唯一信息是 哪些 记录上的 哪些 字段已经发生了变化。

Example

# make sure 'state' is up-to-date in database
self.env['model'].flush_model(['state'])

# use the RETURNING clause to retrieve which rows have changed
self.env.cr.execute("UPDATE model SET state=%s WHERE state=%s RETURNING id", ['new', 'old'])
ids = [row[0] for row in self.env.cr.fetchall()]

# invalidate the cache, and notify the update to the framework
records = self.env['model'].browse(ids)
records.invalidate_recordset(['state'])
records.modified(['state'])

需要找出哪些记录已被修改。有许多方法可以做到这一点,可能涉及额外的 SQL 查询。在上面的示例中,我们利用了 PostgreSQL 的 RETURNING 子句,在不进行额外查询的情况下检索信息。在通过失效使缓存一致之后,使用已更新的字段调用已修改记录上的 modified 方法。

Model.modified(fnames, create=False, before=False)[源代码]

Notify that fields will be or have been modified on self. This invalidates the cache where necessary, and prepares the recomputation of dependent stored fields.

参数
  • fnames – iterable of field names modified on records self

  • create – whether called in the context of record creation

  • before – whether called before modifying records self

常见 ORM 方法

创建/更新

Model.create(vals_list) records[源代码]

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

参数

vals_list (Union[list[dict], dict]) –

values for the model’s fields, as a list of dictionaries:

[{'field_name': field_value, ...}, ...]

For backward compatibility, vals_list may be a dictionary. It is treated as a singleton list [vals], and a single record is returned.

see write() for details

返回

the created records

引发
  • AccessError – if the current user is not allowed to create records of the specified model

  • ValidationError – if user tries to enter invalid value for a selection field

  • ValueError – if a field name specified in the create values does not exist.

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

Model.copy(default=None)[源代码]

Duplicate record self updating it with default values

参数

default (dict) – dictionary of field values to override in the original values of the copied record, e.g: {'field_name': overridden_value, ...}

返回

new records

Model.default_get(fields_list) default_values[源代码]

Return default values for the fields in fields_list. Default values are determined by the context, user defaults, user fallbacks and the model itself.

参数

fields_list (list) – names of field whose default is requested

返回

a dictionary mapping field names to their corresponding default values, if they have a default value.

返回类型

dict

注解

Unrequested defaults won’t be considered, there is no need to return a value for fields whose names are not in fields_list.

Model.name_create(name) record[源代码]

Create a new record by calling create() with only one value provided: the display name of the new record.

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.

参数

name – display name of the record to create

返回类型

tuple

返回

the (id, display_name) pair value of the created record

Model.write(vals)[源代码]

Updates all records in self with the provided values.

参数

vals (dict) – fields to update and the value to set on them

引发
  • AccessError – if user is not allowed to modify the specified records/fields

  • ValidationError – if invalid values are specified for selection fields

  • 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 Boolean, the value should be a bool

  • 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

  • The expected value of a One2many or Many2many relational field is a list of Command that manipulate the relation the implement. There are a total of 7 commands: create(), update(), delete(), unlink(), link(), clear(), and set().

  • For Date and ~odoo.fields.Datetime, the value should be either a date(time), or a string.

    警告

    If a string is provided for Date(time) fields, it must be UTC-only and formatted according to odoo.tools.misc.DEFAULT_SERVER_DATE_FORMAT and odoo.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT

  • Other non-relational fields use a string for value

搜索/读取

Model.browse([ids]) records[源代码]

Returns a recordset for the ids provided as parameter in the current environment.

self.browse([7, 18, 12])
res.partner(7, 18, 12)
参数

ids (int or iterable(int) or None) – id(s)

返回

recordset

Model.search(domain[, offset=0][, limit=None][, order=None])[源代码]

Search for the records that satisfy the given domain search domain.

参数
  • domainA 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

返回

at most limit records matching the search criteria

引发

AccessError – if user is not allowed to access requested information

This is a high-level method, which should not be overridden. Its actual implementation is done by method _search().

Model.search_count(domain[, limit=None]) int[源代码]

Returns the number of records in the current model matching the provided domain.

参数
  • domainA search domain. Use an empty list to match all records.

  • limit – maximum number of record to count (upperbound) (default: all)

This is a high-level method, which should not be overridden. Its actual implementation is done by method _search().

Model.search_fetch(domain, field_names[, offset=0][, limit=None][, order=None])[源代码]

Search for the records that satisfy the given domain search domain, and fetch the given fields to the cache. This method is like a combination of methods search() and fetch(), but it performs both tasks with a minimal number of SQL queries.

参数
  • domainA search domain. Use an empty list to match all records.

  • field_names – a collection of field names to fetch

  • offset (int) – number of results to ignore (default: none)

  • limit (int) – maximum number of records to return (default: all)

  • order (str) – sort string

返回

at most limit records matching the search criteria

引发

AccessError – if user is not allowed to access requested information

Search for records that have a display name matching the given name pattern when compared with the given operator, while also matching the optional search domain (args).

This is used for example to provide suggestions based on a partial value for a relational field. Should usually behave as the reverse of display_name, but that is not guaranteed.

This method is equivalent to calling search() with a search domain based on display_name and mapping id and display_name on the resulting search.

参数
  • name (str) – the name pattern to match

  • args (list) – optional search domain (see search() for syntax), specifying further restrictions

  • operator (str) – domain operator for matching name, such as 'like' or '='.

  • limit (int) – optional max number of records to return

返回类型

list

返回

list of pairs (id, display_name) for all matching records.

Model.fetch(field_names)[源代码]

Make sure the given fields are in memory for the records in self, by fetching what is necessary from the database. Non-stored fields are mostly ignored, except for their stored dependencies. This method should be called to optimize code.

参数

field_names – a collection of field names to fetch

引发

AccessError – if user is not allowed to access requested information

This method is implemented thanks to methods _search() and _fetch_query(), and should not be overridden.

Model.read([fields])[源代码]

Read the requested fields for the records in self, and return their values as a list of dicts.

参数
  • fields (list) – field names to return (default is all fields)

  • load (str) – loading mode, currently the only option is to set to None to avoid loading the display_name of m2o fields

返回

a list of dictionaries mapping field names to their values, with one dictionary per record

返回类型

list

引发
  • AccessError – if user is not allowed to access requested information

  • ValueError – if a requested field does not exist

This is a high-level method that is not supposed to be overridden. In order to modify how fields are read from database, see methods _fetch_query() and _read_format().

Model._read_group(domain, groupby=(), aggregates=(), having=(), offset=0, limit=None, order=None)[源代码]

Get fields aggregations specified by aggregates grouped by the given groupby fields where record are filtered by the domain.

参数
  • domain (list) – A search domain. Use an empty list to match all records.

  • groupby (list) – list of groupby descriptions by which the records will be grouped. A groupby description is either a field (then it will be grouped by that field) or a string 'field:granularity'. Right now, the only supported granularities are 'day', 'week', 'month', 'quarter' or 'year', and they only make sense for date/datetime fields.

  • aggregates (list) – list of aggregates specification. Each element is 'field:agg' (aggregate field with aggregation function 'agg'). The possible aggregation functions are the ones provided by PostgreSQL, 'count_distinct' with the expected meaning and 'recordset' to act like 'array_agg' converted into a recordset.

  • having (list) – A domain where the valid “fields” are the aggregates.

  • offset (int) – optional number of groups to skip

  • limit (int) – optional max number of groups to return

  • order (str) – optional order by specification, for overriding the natural sort ordering of the groups, see also search().

返回

list of tuple containing in the order the groups values and aggregates values (flatten): [(groupby_1_value, ... , aggregate_1_value_aggregate, ...), ...]. If group is related field, the value of it will be a recordset (with a correct prefetch set).

返回类型

list

引发

AccessError – if user is not allowed to access requested information

Model.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) – 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 and ‘count_distinct’, with the expected meaning.

  • groupby (list) – list of groupby descriptions by which the records will be grouped. A groupby description is either a field (then it will be grouped by that field). For the dates an datetime fields, you can specify a granularity using the syntax ‘field:granularity’. The supported granularities are ‘hour’, ‘day’, ‘week’, ‘month’, ‘quarter’ or ‘year’; Read_group also supports integer date parts: ‘year_number’, ‘quarter_number’, ‘month_number’ ‘iso_week_number’, ‘day_of_year’, ‘day_of_month’, ‘day_of_week’, ‘hour_number’, ‘minute_number’ and ‘second_number’.

  • offset (int) – optional number of groups to skip

  • limit (int) – optional max number of groups to return

  • orderby (str) – optional order by specification, for overriding the natural sort ordering of the groups, see also search() (supported only for many2one fields currently)

  • lazy (bool) – if true, the results are only grouped by the first groupby and the remaining groupbys are put in the __context key. If false, all the groupbys are done in one call.

返回

list of dictionaries(one dictionary for each record) containing:

  • the values of fields grouped by the fields in groupby argument

  • __domain: list of tuples specifying the search criteria

  • __context: dictionary with argument like groupby

  • __range: (date/datetime only) dictionary with field_name:granularity as keys

    mapping to a dictionary with keys: “from” (inclusive) and “to” (exclusive) mapping to a string representation of the temporal bounds of the group

返回类型

[{‘field_name_1’: value, …}, …]

引发

AccessError – if user is not allowed to access requested information

字段

Model.fields_get([allfields][, attributes])[源代码]

Return the definition of each field.

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

参数
  • allfields (list) – fields to document, all if empty or not provided

  • attributes (list) – attributes to return for each field, all if empty or not provided

返回

dictionary mapping field names to a dictionary mapping attributes to values.

返回类型

dict

搜索域

域是一个条件列表,每个条件都是一个三元组(可以是 listtuple ),其中: (field_name, operator, value)

  • field_name (str)

    当前模型的字段名称,或通过 Many2one 使用点符号进行关系遍历,例如 'street''partner_id.country'。如果字段是日期(时间)字段,您还可以使用 'field_name.granularity' 指定日期的一部分。支持的粒度包括 'year_number', 'quarter_number', 'month_number', 'iso_week_number', 'day_of_week', 'day_of_month', 'day_of_year', 'hour_number', 'minute_number', 'second_number'。它们都使用整数作为值。

  • operator (str)

    用于将 field_namevalue 进行比较的运算符。有效的运算符有:

    =

    等于

    !=

    不等于

    >

    大于

    >=

    大于或等于

    <

    小于

    <=

    小于或等于

    =?

    未设置或等于(如果 valueNoneFalse ,则返回true,否则的话就像 = 一样)

    =like

    使用 value 模式匹配 field_name 。模式中的下划线 _ 代表(匹配)任意单个字符;百分号 % 匹配零个或多个字符的任意字符串。

    like

    field_name%value% 模式进行匹配。类似于 =like ,但在匹配前将 value 用 ‘%’ 包装

    not like

    不符合 %value% 模式

    ilike

    不区分大小写的 like

    not ilike

    不区分大小写的 not like

    =ilike

    不区分大小写的 =like

    in

    等于 value 中的任何一项, value 应该是一个项目列表

    not in

    value 中的所有项都不相等

    child_of

    value 记录的子级(后代)(value 可以是一个项目或项目列表)。

    考虑模型的语义(即遵循由 _parent_name 指定的关系字段)。

    parent_of

    value 记录的父级(祖先)(value 可以是一个项目或项目列表)。

    考虑模型的语义(即遵循由 _parent_name 指定的关系字段)。

    any

    如果通过 field_name 进行的关系遍历中的任何记录(Many2oneOne2manyMany2many)满足提供的域 value,则匹配。

    not any

    如果通过 field_name``(:class:`~odoo.fields.Many2one`、:class:`~odoo.fields.One2many` :class:`~odoo.fields.Many2many`)的关系遍历中没有记录满足提供的域 ``value,则匹配。

  • value

    变量类型,必须与命名字段通过 operator 进行比较。

域准则可以使用 前缀 形式的逻辑运算符进行组合:

'&'

逻辑 AND,默认操作是将相继的条件组合在一起。Arity 2(使用下2个条件或组合)。

'|'

逻辑 OR,arity 2.

'!'

逻辑 NOT,arity 1.

注解

主要用于否定条件的组合。个别条件通常有一个否定形式(例如 = -> !=< -> >= ),这比否定肯定形式更简单。

Example

搜索名为 ABC 的合作伙伴,其电话或手机号码包含 7620:

[('name', '=', 'ABC'),
 '|', ('phone','ilike','7620'), ('mobile', 'ilike', '7620')]

搜索待开具发票的销售订单,这些订单至少有一行产品的库存不足:

[('invoice_status', '=', 'to invoice'),
 ('order_line', 'any', [('product_id.qty_available', '<=', 0)])]

搜索所有在二月出生的合作伙伴:

[('birthday.month_number', '=', 2)]

记录(集合)信息

Model.ids

Return the list of actual record ids corresponding to self.

odoo.models.env

返回给定记录集的环境。

类型

Environment

Model.exists() records[源代码]

Returns the subset of records in self that exist. It can be used as a test on records:

if record.exists():
    ...

By convention, new records are returned as existing.

Model.ensure_one() Self[源代码]

Verify that the current recordset holds a single record.

引发

odoo.exceptions.ValueErrorlen(self) != 1

Model.get_metadata()[源代码]

Return some metadata about the given records.

返回

list of ownership dictionaries for each requested record

返回类型

list of dictionaries with the following keys:

  • id: object id

  • create_uid: user who created the record

  • create_date: date when the record was created

  • write_uid: last user who changed the record

  • write_date: date of the last change to the record

  • xmlid: XML ID to use to refer to this record (if there is one), in format module.name

  • xmlids: list of dict with xmlid in format module.name, and noupdate as boolean

  • noupdate: A boolean telling if the record will be updated or not

操作

记录集是不可变的,但是可以使用各种集合操作来组合同一模型的集合,返回新的记录集。

  • record in set 返回 record``(必须是一个单元素记录集)是否存在于 ``set 中。record not in set 是相反的操作。

  • set1 <= set2set1 < set2 返回 set1 是否是 set2 的子集 (分别是子集和严格子集)

  • set1 >= set2set1 > set2 返回 set1 是否是 set2 的超集 (分别是严格超集)

  • set1 | set2 returns the union of the two recordsets, a new recordset containing all records present in either source

  • set1 & set2 returns the intersection of two recordsets, a new recordset containing only records present in both sources

  • set1 - set2 返回一个新的记录集,其中仅包含 set2 中的 set1 记录

Recordsets 是可迭代的,因此通常的 Python 工具可用于转换 (map(), sorted(), ifilter(), …) 但是这些返回的是 list 或者 iterator,这样就无法对其结果调用方法或使用集合操作。

因此,记录集提供以下操作,返回记录集本身(如果可能):

筛选

Model.filtered(func) Self[源代码]

Return the records in self satisfying func.

参数

func (callable or str) – a function or a dot-separated sequence of field names

返回

recordset of records satisfying func, may be empty.

# 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")
Model.filtered_domain(domain) Self[源代码]

Return the records in self satisfying the domain and keeping the same order.

参数

domainA search domain.

地图

Model.mapped(func)[源代码]

Apply func on all records in self, and return the result as a list or a recordset (if func return recordsets). In the latter case, the order of the returned recordset is arbitrary.

参数

func (callable or str) – a function or a dot-separated sequence of field names

返回

self if func is falsy, result of func applied to all self records.

返回类型

list or recordset

# 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
records.mapped('partner_id')

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

注解

自V13版本开始,支持多关系字段访问,并且与映射调用类似:

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

排序

Model.sorted(key=None, reverse=False) Self[源代码]

Return the recordset self ordered by key.

参数
  • 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

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

Grouping

Model.grouped(key)[源代码]

Eagerly groups the records of self by the key, returning a dict from the key’s result to recordsets. All the resulting recordsets are guaranteed to be part of the same prefetch-set.

Provides a convenience method to partition existing recordsets without the overhead of a read_group(), but performs no aggregation.

注解

unlike itertools.groupby(), does not care about input ordering, however the tradeoff is that it can not be lazy

参数

key (callable | str) – either a callable from a Model to a (hashable) value, or a field name. In the latter case, it is equivalent to itemgetter(key) (aka the named field’s value)

返回类型

dict

继承和扩展

Odoo 提供了三种不同的机制以模块化的方式扩展模型:

  • 从现有模型创建一个新模型,向副本添加新信息,但保留原始模块不变

  • 在原地扩展在其他模块中定义的模型,替换先前的版本

  • 将模型的一些字段委托给其包含的记录

../../../_images/inheritance_methods1.png

经典继承

当同时使用 _inherit_name 属性时,Odoo 会基于已有的模型 (通过 _inherit 提供)创建一个新的模型。新模型将继承 基础模型的所有字段、方法和元信息(默认值和其他)。

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

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

将产生:

“这是模型0记录A” “这是模型1记录B”

第二个模型继承了第一个模型的 check 方法和 name 字段,但是重写了 call 方法,就像使用标准的 Python 继承 一样。

扩展

当使用 _inherit 但是省略了 _name 时,新模型将替换现有模型,本质上是在原地扩展它。这对于向现有模型(在其他模块中创建的)添加新字段或方法非常有用,或者用于自定义或重新配置它们(例如更改它们的默认排序顺序):

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]

将产生:

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

注解

它还将产生各种 自动字段,除非它们已被禁用

委托

第三种继承机制提供了更大的灵活性(可以在运行时进行修改),但功能较弱:使用 _inherits 属性,一个模型将 委托 查找当前模型上未找到的任何字段 给”子”模型。委托是通过在父模型上自动设置的 Reference 字段来执行的。

主要区别在于含义。使用委托时,模型 有一个 而不是 是一个 ,将关系转变为组合而不是继承:

class Screen(models.Model):
    _name = 'delegation.screen'
    _description = 'Screen'

    size = fields.Float(string='Screen Size in inches')

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

    # a Laptop has a screen
    screen_id = fields.Many2one('delegation.screen', required=True, ondelete="cascade")
    # a Laptop has a keyboard
    keyboard_id = fields.Many2one('delegation.keyboard', required=True, ondelete="cascade")
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})

警告

当使用委托继承时,方法 不会 被继承,只有字段会被继承

警告

  • _inherits is more or less implemented, avoid it if you can;

  • 链式 _inherits 实际上未被实现,我们无法保证最终行为。

字段增量定义

字段是在模型类上定义的类属性。如果模型被扩展,也可以通过在子类上重新定义具有相同名称和相同类型的字段来扩展字段定义。在这种情况下,字段的属性来自父类,并被子类中给定的属性覆盖。

例如,下面的第二个类只在字段 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")

错误管理

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’.

注解

If you consider introducing new exceptions, check out the odoo.addons.test_exceptions module.

exception odoo.exceptions.UserError(message)[源代码]

Generic error managed by the client.

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.

exception odoo.exceptions.RedirectWarning(message, action, button_text, additional_context=None)[源代码]

Warning with a possibility to redirect the user instead of simply displaying the warning message.

参数
  • message (str) – exception message and frontend modal content

  • action_id (int) – id of the action where to perform the redirection

  • button_text (str) – text to put on the button that will trigger the redirection.

  • additional_context (dict) – parameter passed to action_id. Can be used to limit a view to active_ids for example.

exception odoo.exceptions.AccessDenied(message='Access Denied')[源代码]

Login/password error.

注解

No traceback.

示例

When you try to log with a wrong password.

exception odoo.exceptions.AccessError(message)[源代码]

Access rights error.

示例

When you try to read a record that you are not allowed to.

exception odoo.exceptions.CacheMiss(record, field)[源代码]

Missing value(s) in cache.

示例

When you try to read a value in a flushed cache.

exception odoo.exceptions.MissingError(message)[源代码]

Missing record(s).

示例

When you try to write on a deleted record.

exception odoo.exceptions.ValidationError(message)[源代码]

Violation of python constraints.

示例

When you try to create a new user with a login which already exist in the db.