数据文件¶
Odoo 很大程度上是数据驱动的,因此模块定义的很大一部分是它管理的各种记录的定义:UI(菜单和视图)、安全性(访问权限和记录规则)、报告和纯数据都是通过记录定义的。
结构¶
Odoo 中定义数据的主要方式是通过 XML 数据文件: XML 数据文件的大致结构如下:
根元素“
odoo”内任意数量的操作元素
<?xml version="1.0" encoding="UTF-8"?>
<!-- the root elements of the data file -->
<odoo>
<operation/>
...
</odoo>
数据文件是顺序执行的,操作只能引用前面定义的操作结果
注解
如果数据文件的内容预计仅应用一次,您可以指定 odoo 标志 noupdate 设置为 1。如果文件中的部分数据预计仅应用一次,您可以将文件的这部分数据放在 <data noupdate=”1”> 域中。
<odoo>
<data noupdate="1">
<!-- Only loaded when installing the module (odoo-bin -i module) -->
<operation/>
</data>
<!-- (Re)Loaded at install and update (odoo-bin -i/-u) -->
<operation/>
</odoo>
核心业务¶
record¶
record 适当地定义或更新数据库记录,它具有以下属性:
- ``model``(必填)
要创建(或更新)的模型的名称
id该记录的 external identifier。强烈建议提供一份
对于记录创建,允许后续定义修改或引用该记录
对于记录修改,要修改的记录
context创建记录时使用的上下文
forcecreate在更新模式下,如果记录不存在是否应该创建
需要 external id,默认为
True。
field¶
每个记录可以由 field tags, defining values to set when creating the record. A record with no field 组成,将使用所有默认值(创建)或不执行任何操作(更新)。
field has a mandatory name 属性、要设置的字段名称以及定义值本身的各种方法:
- 没有什么
如果没有为该字段提供值,则将在该字段上设置隐式的“
False”。可用于清除字段,或避免使用字段的默认值。search对于 relational fields,应该是字段模型上的 domain。
将评估域,使用它搜索字段的模型并将搜索结果设置为字段的值。如果字段是
Many2one,则仅使用第一个结果ref如果提供了``ref``属性,其值必须是有效的:term:
external id,它将被查找并设置为字段的值。主要针对
Many2one和Reference字段type如果是``type`` attribute is provided, it is used to interpret and convert the field’s content. The field’s content can be provided through an external file using the ``file``属性,或者通过节点的主体。
可用类型有:
xml,html提取``field``’s children as a single document, evaluates any external id specified with the form
%(external_id)s. ``%%``可用于输出实际的*%*符号。file确保字段内容是当前模型中的有效文件路径,将
module,path对保存为字段值char直接将字段内容设置为字段值,无需更改
base64int将字段的内容转换为整数并将其设置为字段的值
float将字段的内容转换为浮点数并将其设置为字段的值
list,tuple应包含任意数量的
valueelements with the same properties asfield,每个元素解析为生成的元组或列表的一项,并且生成的集合设置为字段的值
eval对于前面的方法不适合的情况,
eval属性只是评估它提供的任何 Python 表达式,并将结果设置为字段的值。评估上下文包含各种模块(
time,datetime,timedelta,relativedelta), a function to resolve external identifiers (ref) and the model object for the current field if applicable (obj)
delete¶
delete 标签可以删除先前定义的任意数量的记录。它具有以下属性:
- ``model``(必填)
应删除指定记录的模型
id要删除的记录的 external id
searcha domain 查找要删除的模型的记录
id and search 是独占的
function¶
function tag calls a method on a model, with provided parameters. It has two mandatory parameters model and name 分别指定模型和要调用的方法的名称。
可以使用“eval` (should evaluate to a sequence of parameters to call the method with) or value elements (see ``list`”值来提供参数。
<odoo>
<data noupdate="1">
<record id="partner_1" model="res.partner">
<field name="name">Odude</field>
</record>
<function model="res.partner" name="send_inscription_notice"
eval="[[ref('partner_1'), ref('partner_2')]]"/>
<function model="res.users" name="send_vip_inscription_notice">
<function eval="[[('vip','=',True)]]" model="res.partner" name="search"/>
</function>
</data>
<record id="model_form_view" model="ir.ui.view">
...
</record>
</odoo>
快捷方式¶
由于 Odoo 的一些重要结构模型非常复杂且复杂,因此数据文件提供了使用 record tags 定义它们的更短替代方案:
template¶
创建一个 QWeb view 仅需要视图的 arch 部分,并允许一些*可选*属性:
id该视图的 external identifier
name,inherit_id,priority与
ir.ui.view(nb:inherit_id上的相应字段相同应该是 external identifier)primary如果设置为“
True`and combined with a ``inherit_id`”,则将该视图定义为主视图groups以逗号分隔的组 external identifiers 列表
active指示视图是否处于活动状态。如果处于非活动状态,则不会应用其 XPath 规则,因此这主要与具有“
inherit_id”的视图相关注解
定义
activevalue on the<template>node itself comes with a subtlety: it is only considered when creating the record. On subsequent updates, the view will be updated but not itsactive状态。
asset¶
创建一个 asset。
Example
<asset id="website_something.some_style_asset" name="Some style asset" active="False">
<bundle>web.assets_frontend</bundle>
<path>website_something/static/src/some_style.scss</path>
</asset>
属性
idname与``ir.asset``上的相应字段相同
- ``active``(可选)
指示资产是否活跃
注解
与
<template>, defining theactivevalue on the<asset>node itself comes with a subtlety: it is only considered when creating the record. On subsequent updates, the asset will be updated but not itsactive状态相同。
子元素
CSV 数据文件¶
XML 数据文件非常灵活且具有自描述性,但在批量创建同一模型的多个简单记录时非常冗长。
对于这种情况,数据文件也可以使用 csv,这通常是 access rights 的情况:
文件名是
model_name.csv第一行列出了要写入的字段,其中特殊字段“
id”代表 :term:`external identifiers`(用于创建或更新)此后的每一行都会创建一个新记录
这是定义国家/地区“res.country.state.csv”的数据文件的第一行
"id","country_id:id","name","code"
state_au_1,au,"Australian Capital Territory","ACT"
state_au_2,au,"New South Wales","NSW"
state_au_3,au,"Northern Territory","NT"
state_au_4,au,"Queensland","QLD"
state_au_5,au,"South Australia","SA"
state_au_6,au,"Tasmania","TAS"
state_au_7,au,"Victoria","VIC"
state_au_8,au,"Western Australia","WA"
state_us_1,us,"Alabama","AL"
state_us_2,us,"Alaska","AK"
state_us_3,us,"Arizona","AZ"
state_us_4,us,"Arkansas","AR"
state_us_5,us,"California","CA"
state_us_6,us,"Colorado","CO"
以更易读的格式呈现:
ID |
国家ID:ID |
姓名 |
代码 |
|---|---|---|---|
州_au_1 |
非盟 |
澳大利亚首都特区 |
行为 |
状态_au_2 |
非盟 |
新南威尔士州 |
新南威尔士州 |
状态_au_3 |
非盟 |
北领地 |
新台币 |
州_au_4 |
非盟 |
昆士兰州 |
昆士兰州 |
州_au_5 |
非盟 |
南澳大利亚 |
SA |
州_au_6 |
非盟 |
塔斯马尼亚 |
TAS |
州_au_7 |
非盟 |
维多利亚 |
维克 |
州_au_8 |
非盟 |
西澳大利亚州 |
西澳 |
州_us_1 |
我们 |
阿拉巴马州 |
AL |
州_us_2 |
我们 |
阿拉斯加州 |
AK |
州_us_3 |
我们 |
亚利桑那 |
AZ |
州_us_4 |
我们 |
阿肯色州 |
增强现实 |
州_us_5 |
我们 |
加利福尼亚州 |
CA |
州_us_6 |
我们 |
科罗拉多州 |
一氧化碳 |
对于每一行(记录):
第一列是要创建或更新的记录的 external id
第二列是要链接到的国家/地区对象的 :term:`external id`(必须事先定义国家/地区对象)
第三列是“
name`field for ``res.country.state`”第四列是“
code`field for ``res.country.state`”