Fields

Basic fields

class openerp.fields.Field(string=None**kwargs)

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

Parameters
  • 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 -- the default value for the field; this is either a static value, or a function taking a recordset and returning a value
  • 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 -- comma-separated list of group xml ids (string); this restricts the field access to the users of the given groups only
  • 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)
  • 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.

Parameters
  • compute -- name of a method that computes the field
  • inverse -- name of a method that inverses the field (optional)
  • search -- name of a method that implement search on the field (optional)
  • 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)

The methods given for computeinverse 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 openerp.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.

The search method is invoked when processing domains before doing an actual search on the model. It must return a domain equivalent to the condition: field operator value.

Related fields

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

Parameters
related -- sequence of field names

Some field attributes are automatically copied from the source field if they are not redefined: stringhelpreadonlyrequired (only if all fields in the sequence are required), groupsdigitssizetranslatesanitizeselectioncomodel_namedomaincontext. All semantic-free attributes are copied from the source field.

By default, the values of related fields are not stored to the database. Add the attribute store=True to make it stored, just like computed fields. Related fields are automatically recomputed when their dependencies are modified.

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.

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

For instance, the second class below only adds a tooltip on the field state:

class First(models.Model):
_name = 'foo'
state = fields.Selection([...], required=True) class Second(models.Model):
_inherit = 'foo'
state = fields.Selection(help="Blah blah blah")
class openerp.fields.Char(string=None**kwargs)

Bases: openerp.fields._String

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

Parameters
  • size (int) -- the maximum size of values stored for that field
  • translate (bool) -- whether the values of this field can be translated
class openerp.fields.Boolean(string=None**kwargs)

Bases: openerp.fields.Field

class openerp.fields.Integer(string=None**kwargs)

Bases: openerp.fields.Field

class openerp.fields.Float(string=Nonedigits=None**kwargs)

Bases: openerp.fields.Field

The precision digits are given by the attribute

Parameters
digits -- a pair (total, decimal), or a function taking a database cursor and returning a pair (total, decimal)
class openerp.fields.Text(string=None**kwargs)

Bases: openerp.fields._String

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

Parameters
translate -- whether the value of this field can be translated
class openerp.fields.Selection(selection=Nonestring=None**kwargs)

Bases: openerp.fields.Field

Parameters
  • selection -- specifies the possible values for this field. It is given as either a list of pairs (valuestring), 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 (valuestring).

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

class openerp.fields.Html(string=None**kwargs)

Bases: openerp.fields._String

class openerp.fields.Date(string=None**kwargs)

Bases: openerp.fields.Field

static context_today(recordtimestamp=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.

Parameters
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.)
Return type
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 openerp.fields.Datetime(string=None**kwargs)

Bases: openerp.fields.Field

static context_timestamp(recordtimestamp)

Returns the given timestamp converted to the client's timezone. This method is not meant for use as a _defaults initializer, because datetime fields are automatically converted upon display on client side. For _defaults you fields.datetime.now()should be used instead.

Parameters
timestamp (datetime) -- naive datetime value (expressed in UTC) to be converted to the client timezone
Return type
Returns
timestamp converted to timezone-aware datetime in context timezone
static from_string(value)

Convert an ORM value into a datetime value.

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.

Relational fields

class openerp.fields.Many2one(comodel_name=Nonestring=None**kwargs)

Bases: openerp.fields._Relational

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

Parameters
  • comodel_name -- name of the target model (string)
  • domain -- an optional domain to set on candidate values on the client side (domain or string)
  • context -- an optional context to use on the client side when handling that field (dictionary)
  • ondelete -- what to do when the referred record is deleted; possible values are: 'set null''restrict''cascade'
  • auto_join -- whether JOINs are generated upon search through that field (boolean, by default False)
  • delegate -- set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

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

class openerp.fields.One2many(comodel_name=Noneinverse_name=Nonestring=None**kwargs)

Bases: openerp.fields._RelationalMulti

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

Parameters
  • comodel_name -- name of the target model (string)
  • inverse_name -- name of the inverse Many2one field in comodel_name (string)
  • domain -- an optional domain to set on candidate values on the client side (domain or string)
  • 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)

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

class openerp.fields.Many2many(comodel_name=Nonerelation=Nonecolumn1=Nonecolumn2=Nonestring=None**kwargs)

Bases: openerp.fields._RelationalMulti

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

Parameters
comodel_name -- name of the target model (string)

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

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

The attributes relationcolumn1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!

Parameters
  • domain -- an optional domain to set on candidate values on the client side (domain or string)
  • context -- an optional context to use on the client side when handling that field (dictionary)
  • limit -- optional limit to use upon read (integer)
class openerp.fields.Reference(selection=Nonestring=None**kwargs)

Bases: openerp.fields.Selection

Odoo Documentation : Fields的更多相关文章

  1. Odoo Documentation : Recordsets

    Other recordset operations Recordsets are iterable(可迭代的) so the usual Python tools are available for ...

  2. Odoo Documentation : Environment

    Environment The Environment stores various contextual data(上下文数据 ) used by the ORM: the database cur ...

  3. Defining custom settings in Odoo

    Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...

  4. odoo配置界面设置字段默认值

    转自国外牛人博客:http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html Defining custom settings in O ...

  5. Odoo 12 开发手册指南(八)—— 业务逻辑 – 业务流程的支持

    在前面的文章中,我们学习了模型层.如何创建应用数据结构以及如何使用 ORM API 来存储查看数据.本文中我们将利用前面所学的模型和记录集知识实现应用中常用的业务逻辑模式. 本文的主要内容有: 以文件 ...

  6. odoo 11 实现多个字段对应一个查询参数的查询

    在整理英语单词开发模块的过程中,有这样一个需求,就是我在查询界面里输入一个查询的值A,这个A可能是下面的任何一个值 1.一个英语单词  2.汉语文字  3.一个英语单词的部分 这里有两张表:engli ...

  7. odoo 有哪些文档资源

    // openbook [覆盖 openerp 7 及之前版本] https://doc.odoo.com/     // 最新的 odoo documentation user[覆盖 odoo 9] ...

  8. 第十三章 Odoo 12开发之创建网站前端功能

    Odoo 起初是一个后台系统,但很快就有了前端界面的需求.早期基于后台界面的门户界面不够灵活并且对移动端不友好.为解决这一问题,Odoo 引入了新的网站功能,为系统添加了 CMS(Content Ma ...

  9. 第八章 Odoo 12开发之业务逻辑 - 业务流程的支持

    在前面的文章中,我们学习了模型层.如何创建应用数据结构以及如何使用 ORM API 来存储查看数据.本文中我们将利用前面所学的模型和记录集知识实现应用中常用的业务逻辑模式. 本文的主要内容有: 以文件 ...

随机推荐

  1. Oracle大数据查询优化

    1.对于像状态之类的列,不是很多的,就可以加位图索引,对于唯一的列,就加唯一索引,其余的创建普通索引. 2.尽量不要使用select * 这样的查询,指定需要查询的列. 3.使用hits  selec ...

  2. 思维题+栈的应用——cf1092D有意思

    第一例很简单,把两个差为偶数的列不断合并即可 这种不需要撤销的合并相连数直接用栈来做 /* 如果相邻两列高度差为偶数 那么可以直接消去 */ #include<bits/stdc++.h> ...

  3. VC++ MFC文件的移动复制删除更名遍历操作

    1.判断文件是否存在 利用CFile类和CFileStatus类判断 CFileStatus filestatus; if (CFile::GetStatus(_T("d://softist ...

  4. C++Builder常用函数

    BCB函数集 1.内存分配     函数名称 AllocMem 函数说明 在队中分配指定字节的内存块,并将分配的每一个字节初始化为 0.函数原型如下: void * __fastcall AllocM ...

  5. iOS疑问

    1.__NSFrozenDictionaryM在数组类簇中是什么角色?

  6. Celery - 异步任务 , 定时任务 , 周期任务

    1.什么是Celery?Celery 是芹菜Celery 是基于Python实现的模块, 用于执行异步定时周期任务的其结构的组成是由    1.用户任务 app    2.管道 broker 用于存储 ...

  7. spring加载属性配置文件内容

    在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...

  8. MATLAB 中自定义函数的使用

    MATLAB在文件内部(在函数内部)定义函数,但文件名以开头函数来命名,与Java中每个文件只能有一个公开类,但在文件内部还是可以定义其他非公开类一个道理. 无参函数 do.m function do ...

  9. JS规则 我还有其它用途( +号操作符)例如,算术操作符(+、-、*、/等),比较操作符(<、>、>=、<=等),逻辑操作符(&&、||、!)

    我还有其它用途( +号操作符) 操作符是用于在JavaScript中指定一定动作的符号. (1)操作符 看下面这段JavaScript代码. sum = numa + numb; 其中的"= ...

  10. docker 个人遇到问题日志记录

    system: openSUSE Leap 42.3 在openSUSE中可直接运行" sudo zypper in docker"进行安装docker-ce wakasann@l ...