源地址:http://www.odoo.com/documentation/9.0/reference/data.html#structure
Odoo诸多是由数据驱动的。大部分模块是由他所管理的不同种类的records所定义的:UI(菜单和views),安全(accres rights和访问,链接规则),报表和普通的数据都是由records来定义的。
结构
通过XML文件来定义数据是Odoo中的主要方式:下面是广泛的XML数据文件的机构:
- 任意的操作元素都在根元素odoo中
<!-- the root elements of the data file --> <odoo> <operation/> ... </odoo>
数据文件被有序的执行,操作只能引用,关系之前定义的操作的结果。
核心操作
record
record 定义或者更新一条数据库记录,他拥有以下属性:
model(必须)
所要创建或更新的model名称
id
the external identifier for this record(这条记录的外部识别符).强烈推荐提供一个id
- 对于创建record,允许之后的定义修改或者引用关系这个record。
- for record modification,the record to modify
context
record创建的上下文
forcecreate
在更新一个model时如果他不存在,这条记录将会被创建。
Requires an external id, defaults to
True
.(需要一个外部id,默认为True)field
每个record都是由field标签组成,创建记录的时候向set中添加值。一条没有field的record将会在create时全部使用默认值或在update的时候不做任何事情。
一个field有一个必须的强制的name属性,他是注入的这个field的名称。同时还有各种方法来定义他的值。
Nothing
如果这个field没有值,False将会被赋予这个field。这样能够用来清空一个field或者避免给field赋予默认值。
search
for relational fields, should be a domain on the field's model.
因为relational fields,field's model需要是一个domain
Will evaluate the domain, search the field's model using it and set the search's result as the field's value. Will only use the first result ifthe field is a
Many2one
利用他查找field的model,并且把查找的结果作为field's value。将只会使用第一个结果如果这个field是many2one的话。
ref
如果提供了,存在一个ref属性,他的value必须是一个有效的external id,他能够被查出来并且能够被作为fields值被set。
大多数情况下用来作为
Many2one
andReference
fields。
type
如果存在一个type属性,他被用来解释或者convert field的内容。field的内容能够通过file属性来引用一个扩展的文件来提供,或者通过node‘s body来提供。
下面是一些可用的types:
xml,html
extracts the
field
's children as a single document, evaluatesany external id specified with the form%(external_id)s
.%%
can be used to output actual % signs.file
- 确保这个field的内容是一个当前model下有效的文件路径,将module和path作为field的值成对保存。
char- 将field的value直接赋给field的content,不做修改。
base64- 把field的content作为base64编码,能够有效的结合file属性来加载例如图片数据等到附件中。
int
- convert field的content to Integer 类型并将其设为field's value。
float- 类似于int。
list,tuple- should contain any number of
evalvalue
elements with the sameproperties asfield
, each element resolves to an item of agenerated tuple or list, and the generated collection is set as thefield's value
for cases where the previous methods are unsuitable, the
eval
attributes simply evaluates whatever Python expression it is provided andsets the result as the field's value.The evaluation context contains various modules (
time
,datetime
,timedelta
,relativedelta
), a function to resolveexternalidentifiers (ref
) and the model object for the current field if applicable (obj
)
delete
delete标签能够去除任何之前定义的records,它有以下的属性:
mode(必须)
指定被删除的记录的model
id
将要移除记录的external id
search
a domain来查询要移除的record
id和search是exclusive的。
function
function标签用所提供的参数调用了model上的方法。他必须由两个参数,model和name。分别指定调用的是哪个model里的哪个方法。
能够通过eval(需要评估出调用方法所用的参数作为一个序列)或value元素(参考list value)来提供参数。
workflow
workflow标签给现有的workflow传送一个信号。workflow能够通过ref 属性或一个返回workflow id(现有workflow的external id)的value 标签来被指定。
workflow标签也有两个必要的属性,mode(关联workflow的model)和action(传送给workflow的信号的名称)。
shortcuts
因为一些重要的结构化的odoo的moel比较复杂,data files 用提供了简短的选择来定义他们,利用以下标签:
menuitem
用一系列的默认值和fallbacks来定义ir.ui.menu
:
parentmenu
- 若他的parent属性是set,需要提供另一个menu的external id作为新item‘s parent。
- If no
parent
is provided, tries to interpret thename
attributeas a/
-separated sequence of menu names and find a place in the menuhierarchy. In that interpretation, intermediate menus are automaticallycreated
- Otherwise the menu is defined as a "top-level" menu item (not a menuwith no parent)
menu name
如果name属性没有被指定,如果由关联的action,将会从关联的action中获取menu的nanme。否则将会使用rcords的id。
groups
A groups
attribute is interpreted as a comma-separated sequence of external identifiers for res.groups
models. If an external identifier is prefixed with a minus (-
), the groupis removed from the menu's groups
action
action属性应该是一个action的external id,当menu被打开的时候执行这个action。
id
menu item的external id
template
创建一个QWeb view只必须view的arch部分,且提供了下面的可选属性:
id
view的external identifier
name,inherit_id,priority
same as the corresponding field on ir.ui.view
(nb: inherit_id
should be an external identifier)
primary
如果设置为True且结合了一个inherit_id,则把他定义为首要的。
groups
comma-separated list of group external identifiers
page
如果设定为true,template是一个网站页面(可链接,可删除)。
optional
enabled
or disabled
, whether the view can be disabled (in thewebsite interface) and its default status. If unset, the view is alwaysenabled
report
利用一些默认value创建一个ir.actions.report.xml
record。
Mostly just proxies attributes to the corresponding fields onir.actions.report.xml
, but also automatically creates the item in theMore menu of the report's model
.
CSV data files
XML data files are flexible and self-descriptive, but very verbose whencreating a number of simple records of the same model in bulk.
For this case, data files can also use csv, this is often the case for access rights:
- the file name is
model_name.csv
- the first row lists the fields to write, with the special field
id
for external identifiers (used for creation or update) - each row thereafter creates a new record
Here's the first lines of the data file defining US statesres.country.state.csv