Odoo 16 中新增的 Properties 字段
2022年11月27日 08:00
Odoo 16 正式发布已经有一段时间了,最近也在基于最新的版本做了一些开发,顺便学习了一下
Owl
并且开发了个小模块作为练习。在翻看 Project
这个应用的时候,发现在模型 project.task
上有一个 Properties
类型的字段,在之前的版本中是没有见到过的,于是翻阅了一下源码,在这里做个记录。""" Field that contains a list of properties (aka "sub-field") based on a definition defined on a container. Properties are pseudo-fields, acting like Odoo fields but without being independently stored in database. This field allows a light customization based on a container record. Used for relationships such as <project.project> / <project.task>,... New properties can be created on the fly without changing the structure of the database. The "definition_record" define the field used to find the container of the current record. The container must have a :class:`~odoo.fields.PropertiesDefinition` field "definition_record_field" that contains the properties definition (type of each property, default value)... Only the value of each property is stored on the child. When we read the properties field, we read the definition on the container and merge it with the value of the child. That way the web client has access to the full field definition (property type, ...). """
在
odoo/fields.py
中可以找到 Properties
类的定义,从注释里可以大致了解这个字段类型的一些基本信息:- 适合轻度的自定义需求
- 字段包含了“容器”定义好的属性
- 这些属性表现和 Odoo 字段一样但是并不单独存储在数据库中(数据库表中不会有对应的列)
- 在一组有关联关系的模型中使用,其中一方为“容器”(如
project.project
和porject.task
的关系)
- 需要配合“容器”中定义的
PropertiesDefinition
类型字段一起使用(存储了属性的定义)
- 属性的值会被存储在子记录中(和“容器”关联的记录,定义了
Properties
字段的模型的记录)
- 读取字段时会将“容器”的属性定义和子记录的字段值合并
实际看一下例子可能会更加容易理解这里面提到的一些概念,我们可以直接安装好项目模块,然后打开任意的任务记录,随意添加一些自定义属性试试。

上面截图中添加了三个属性,其中两个为
Char
一个为 Many2one
类型,分别来看一下属性的定义和存储的属性值是怎样的# project.project 的 task_properties_definition 字段中存储的属性定义 [ {...
剩余内容已隐藏
查看完整文章以阅读更多