Odoo 是一個高度數據驅動的系統,他的 UI (menus and views),security (access right and aceess rule),reports 以及 plain data 都是通過 定義 record 來實現的
Structure
在 Odoo 中設置一些數據的最主要的方式就是通過設置 XML data files,它的結構如下:
<!-- the root elements of the data file -->
<odoo>
<operation/>
...
</odoo>
Data files 都是按照順序讀取執行的,所以當某個 operation 引用 另一個 operation 的時候,必需在 它的後面定義。
Core operations
record
record
就像它的名字一樣,就是用來更新或者新建一條數據的,有下列屬性:
model
(required ),創建或更新的數據是哪一個 Odoo Model 的id
這個 record 的 external identifier,強烈建議設置
- 如果是創建, 可以幫助其它想要修改這條record,用來引用它
- 如果是更新, 就直接通過查詢 erxternal identifier 來找到它
context
context to use when creating the recordforcecreate
如果沒有這個對應的 record id,那麼就創建一個新的record,且 external id 爲提供的id,默認是 True
field
每一個 record 都是有若干個 field
tag 組成的,定義創建一個 record時,它的各項字段的值。 一個 record
如果沒有 field
字段,那麼就使用字段的默認值創建(create ),或者不做任何操作(update )。
一個 field
有一個必需的設置的屬性 name
, 指明是爲哪一個字段設置值
Nothing
如果這個字段沒有設置任何值<field name='xxx'></field>
,那麼將會爲這個field的值設置爲False,可以用來清空某個字段的值,或者新建 record 時,避免使用這個field的默認值。search
中在 relational field 中使用,應該設置爲一個 domain,通過執行domain,得到返回的值,並將值賦給這個 field。如果這個字段是 Many2one, 那麼就使用查詢後返回的 第一個值 作爲這個field的值。ref
如果設置ref
屬性,那麼 ref 的值就必須是一個 external identifier,那個對應 id 的record 就會被設置爲本 record的此field的值,通常是在 Many2one field 中使用。type
是用來解釋和轉換的 field’s content。可以設置爲:
xml
,html
將 field tag 的子節點,當成一個document,可以通過%(external_id)s
在 form tag 中轉換任意的 external id。%%
可以表示一個 百分號符號。file
確保 field tag 的 content 是一個有效的 file path,這個字段的值爲被設置成module,path
。char
將 field tag 的content直接設爲這個field的值,不經過任何轉換base64
base64-encodes the field’s content, useful combined with the file attribute to load e.g. image data into attachmentsint
content 轉成 int 後, 賦值float
content 轉成 float 後, 賦值list
,tuple
should contain any number of value elements with the same properties as field, each element resolves to an item of a generated tuple or list, and the generated collection is set as the field’s value
eval
爲如果上面的幾種屬性不符合你的特殊情況。可以使用這個,它基本和 python eval 一樣,將任意表達式執行後的值賦值給 這個 field。
這個 eval 還帶有一些context, (time
,datetime
,timedelta
,relativedelta
)。以及一個用來轉換 external id的函數ref
,以及當前field 的model objobj
。
delete
可以刪除之前定義過的,任意數目的 records,有如下屬性:
model
(required) 指定要刪除的record 是哪一個 model 下的。id
刪除指明 external idsearch
刪除符合 search domain 的。 records
id
和 search
不能同時設置。
function
將會調用一個model的 method,有兩個屬性必需設置,name
和 model
,分別指明 method的name 和 這個 method 是哪一個 model 下的。method 的參數可以通過 設置 eval 屬性來傳遞,但是eval的結果必需是一個 sequence,或者 通過 value
tag 來實現。
workflow
workflow
tag 用以傳遞一個信號到已經存在的 workflow 中
The workflow tag sends a signal to an existing workflow. The workflow can be specified via a ref attribute (the external id of an existing workflow) or a value tag returning the id of a workflow.
The tag also has two mandatory attributes model (the model linked to the workflow) and action (the name of the signal to send to the workflow).
Shortcuts
由於Odoo的一些model是非常複雜的,所以提供了另一種相比通過record更加簡單的方式來定義他們。
minuteman
通過一系列的默認和預設的操作,來定一個ir.ui.menu
record。
- Parent menu
- 如果設置了
parent
屬性,那麼它的值應該是另一個 menu item 的 external id - 如果沒有設置
parent
,將會嘗試從name
屬性中 按照/
來分隔,找到對應的層級來創建,如果找不到,就依次創建對應的 menu item。 - 如果
name
中沒有/
,那麼就把這個menu 作爲一個top-level
的menu 。
- 如果設置了
- Menu name
如果沒有設置name
屬性,那麼就從這個 menu 對應的action
的 name,如果沒有 action,就使用id
。 - Groups
groups
的值是res.groups model 下的group 對應的 external id 由 逗號分割的 string。如果某個 external id 前面有-
,那麼就是把它從 menu groups 中刪除。
<menuitem groups='sales.group_manager,-sales.group_see_own_leads'
。 action
就是點擊這個menuitem 時觸發 action 的對應的 external idid
這個 menuitem 的 external id。
template
Creates a QWeb view requiring only the arch section of the view, and allowing a few optional attributes:
id
view 的 external idname
,inherit_id
,priority
和普通的ir.ui.view
是一樣的效果primary
, 如果設置爲 True, 並且有inherit_id
, 使得這個view 爲 primarygroups
, 逗號分割的 external idpage
,如果設置爲 True,使得這個 template 是一個 website page,(可鏈接,可刪除)optional
,
值爲enabled
ordisabled
,使得這個view 是否可以在 website 界面被禁用。如果沒有設置,那麼它的值就是enabled
report
用一些默認值來創建一個ir.actions.report.xml
Mostly just proxies attributes to the corresponding fields on ir.actions.report.xml, but also automatically creates the item in the More menu of the report’s model.
CSV data files
XML data files 是靈活,但是當你想要爲某個 model 建立一大堆數據的時候,就顯得特別的繁瑣冗長。
爲了解決這種情況,data files 也可以用 CSV 的方式定義,它通常用來設置 access right.
- 文件的名字是
model_name.csv
- 第一行是要設置數據的field name,但是第一列必需是
id
, 用來確定是 update 還是 create - 之後的每一行都代表一個 record
如果要創建 state 可以這樣創建一個文件 名字爲 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"
每一行(record )
- 第一列,external id, 用以明確是 create 還是 update
- 第二列,country object 的 external id。 注意 country 必需先定義
- 第三列,
name
field forres.country.state
- 第四列,
code
field forres.country.state