樹視圖
當我們在一個清單視圖中和查詢頁面中工作時将使用這些視圖(為了馬上看到多種資源)。這些視圖比form表單簡單多了并有更少的選項。
查詢視圖
查詢視圖是OpenERP 6.0以上的新功能。其建立了定制化的查詢面闆,并且與表單視圖聲明很相似,除了試圖類型和根元素變為search,而不是form
下面的是查詢視圖中支援的一列新元素和功能。
Group tag(組标簽)
不像組元素,查詢視圖分組支援一行中不限定數量的條件(字段或過濾器),并且僅适用以下屬性。
- expand : 開啟分組中的擴充器圖示(預設是1是擴充,0是縮減)
- string : 分組的标簽
<group expand="1" string="Group By">
<filter string="Users" icon="terp-project" domain="[]" context="{'group_by':'user_id'}"/>
<filter string="Project" icon="terp-project" domain="[]" context="{'group_by':'project_id'}"/>
<separator orientation="vertical"/>
<filter string="Deadline" icon="terp-project" domain="[]" context="{'group_by':'date_deadline'}"/>
</group>
上述是一個擴充分組
Filter tag(過濾器标簽)
過濾器在查詢面闆過濾器元素中以一個切換按鈕顯示。過濾器元素能在目前領域或者查詢視圖上下文中添加新的值。過濾器也可以作為一個子元素添加,指明它們專門滿足這個過濾器(這種情況下按鈕圖示将很小)
在下面的圖檔中,紅色部分包含了表單頂層的過濾器,同時藍色部分高亮顯示了一個字段和其子過濾器。
<filter string="Current" domain="[('state','in',('open','draft'))]" help="Draft, Open and Pending Tasks" icon="terp-project"/>
<field name="project_id" select="1" widget="selection">
<filter domain="[('project_id.user_id','=',uid)]" help="My Projects" icon="terp-project"/>
</field>
Group by (分組)
<filter string="Project" icon="terp-project" domain="[]" context="{'group_by':'project_id'}"/>
上面的過濾器分組記錄了共享了同樣的project_id值。分組是延遲加載的,是以當分組擴充時,内部分組才加載。分組頭行包含了那個分組中所有記錄的普通值,并且視圖中目前顯示的數字字段被那個分組的值得總和替代。
它也可能通過指定一列字段而不是一個字元串根據多個值分組。這種情況下将顯示嵌套分組。
<filter string="Project" icon="terp-project" domain="[]" context="{'group_by': ['project_id', 'user_id'] }"/>
Fileds(字段)
視圖中的字段元素用于擷取使用者提供的值來搜尋。結果是,作為分組元素,其與表單視圖字段不同。
- 一個查詢字段可以擷取過濾器,一般都包含過濾器和字段并且管理相同和關聯的字段。 那些内部過濾器渲染成一個小的按鈕,靠近這個字段的右邊,并且不必有一個String屬性
- 一個查詢字段建構了一個領域,由[(field_name,operator,field_value)]. 這個領域可以通過兩種方式重寫:@operator 替換了這個字段的預設操作符(取決于它的類型)。@filter_domain 讓你提供一個完全自定義的領域,替換了預設領域的建立。
- 一個查詢字段預設地不建立一個上下文,但是了你可以提供一個@context,其将評估和平成更加寬泛的上下文(與一個過濾器元素一起)
為了擷取@context 或@filter_domain字段的值,可以使用變量self:
<field name="location_id" string="Location"
filter_domain="['|',('location_id','ilike',self),('location_dest_id','ilike',self)]"/>
或者
<field name="journal_id" widget="selection"
context="{'journal_id':self, 'visible_id':self, 'normal_view':False}"/>
Range fileds(date,datetime,time)區間字段
區間字段由兩個小部件組成(from和and),而不是僅僅一個。
這就導緻了一些特性(與非區間字段對比):
- 不能通過@operator重寫區間字段的操作符。作為一個領域由兩個部分構成并且每個部分使用一個不同的操作符
- 不是一個簡單的值(integer,string,float)在@filter_domain中使用self并且@context是一個字典
因為一個區間字段的每個輸入部件可能為空(但字段本身任舊有效),當使用self的時候必須注意:其有兩個字元串的關鍵字”from“和”to“,但是了這些關鍵字的任何一個可能完全沒有或者設定值為false。
Actions for Search view(搜尋視圖的動作)
聲明了一個搜尋視圖後,它将自動用于同一模式下的所有樹形視圖。如果單一模式下有多個搜尋視圖,将使用有最高優先級的那個(按照最低排序)。另一個選項是明确地選擇你想使用的搜尋視圖,通過設定那個動作的search_view_id 字段。
另外,為了在action的上下文中傳遞預設的表單值,Operator6.0現在支援為查詢視圖傳遞初始化值。上下文關鍵字需要比對search_default_XXX格式,xxx可能與查詢視圖中的一個字段或者過濾器的名字關聯(由于過濾器的name屬性不是必須的,這裡隻是為了過濾器有一個精确的名字設定)。這個值應該是查詢字段的初始化值,或者僅僅是過濾器的布爾值來切換它們。
<record id="action_view_task" model="ir.actions.act_window">
<field name="name">Tasks</field>
<field name="res_model">project.task</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar,gantt,graph</field>
<field eval="False" name="filter"/>
<field name="view_id" ref="view_task_tree2"/>
<field name="context">{"search_default_current":1,"search_default_user_id":uid}</field>
<field name="search_view_id" ref="view_task_search_form"/>
</record>
Custom Filters (定制化過濾器)
6.0版本,所有的查詢視圖有定制化查詢過濾器功能,如下所示。使用者可以使用目前模式的可用字段的一些,使用AND/OR操作符連接配接來定義自己的過濾器。也可能儲存任何查詢上下文(目前比對的領域和上下文值得組合)作為定制的過濾器,可以在任何時候調用。過濾器也可以在使用者首頁中直接簡化變成可用的。
在上述例子中,我們使用Salesman = Demo user 和 Country = Belgium 過濾partner。我們可以儲存查詢條件作為一個快捷鍵或者以過濾器儲存。
過濾器是使用者指定的并且可以在過濾器的下拉清單中借助過濾器管理選項修改
Graph views(圖表過濾器)
對于表單類型的視圖,圖示是一種新的視圖模式。例如,如果線上的銷售訂單必須以清單或者圖示顯示,在action(線上打開銷售訂單的動作)中如此定義。不要這樣設定視圖模式("tree,form,graph" or "form,graph”)-必須是“graph,tree”來首先顯示圖示或者“tree,graph”來首先顯示清單。(視圖模式對于“form,tree”視圖多餘的并且應該有一個獨立的菜單項)。
<field name="view_type">form</field>
<field name="view_mode">tree,graph</field>
view_type(視圖類型)
tree = (tree with shortcuts at the left), form = (switchable view form/list)
view_model(模式)
tree,graph : sequences of the views when switching(轉換時視圖順序)
稍後,使用者将可以從一個視圖轉換到另一個視圖。不像表單和樹,OpenERP不能為圖示類型根據需要自動建立視圖,是以了你必須定義了這個圖表的視圖
<record model="ir.ui.view" id="view_order_line_graph">
<field name="name">sale.order.line.graph</field>
<field name="model">sale.order.line</field>
<field name="type">graph</field>
<field name="arch" type="xml">
<graph string="Sales Order Lines">
<field name="product_id" group="True"/>
<field name="price_unit" operator="*"/>
</graph>
</field>
</record>
圖表視圖
圖表類型視圖隻是圖表的一列字段
圖示簽
圖表的預設類型是一個餅圖--可以通過以下代碼變成條形餅圖,将<graph string="Sales Order Lines">改為<graph string="Sales Order Lines" type="bar"> 。你也可能改變這個操作方向 垂直或者水準方向。
例子
<graph string="Sales Order Lines" orientation="horizontal" type="bar">
Field tag (字段标簽)
第一個字段是X軸。第二個是Y軸,第三個是Z軸,這是對應三維圖的。
- group:如果設定為true,用戶端将對這個字段的相同值的所有條目分為一組。對于每個其他的字段,其将應用一個操作符。
- operator(操作符):應用的操作符是另一個分組字段。預設是“+”。允許的字段是:+(加法),*(乘法),**(指數運算),min(清單的最小數量),max(清單的最大數量)
定義對象的統計
--------------------
-------------------
計算對象統計的最簡單的方法是:
- 定義一個統計對象,其是一個PostgreSQL視圖
- 建立這個對象的樹視圖和圖表視圖
你可以擷取所有子產品表單的一個例子:例如,report_crm
控制視圖動作
當定義一個視圖,下面的元素可以添加到視圖的打開的元素中。(比如<form>,<tree>...)
create
設定為false來隐藏那個連接配接/按鈕,允許你建立了一個新的記錄
delete
設定為false來隐藏連接配接/按鈕,允許你移除一個記錄。
edit
設定false來隐藏連接配接/按鈕,允許你編輯一個記錄。
這些屬性對于form,tree,kanban和gantt視圖都是可用的。它們通常根據使用者的通路權限自動加載,但是了可以在視圖定義中全局性強制設定。針對這些屬性一個可能的使用情況是在一個表單視圖内定義一個針對one2many關系的内部樹視圖,在其内部不能添加或者移除先關的記錄,但是可以編輯存在的(可以通過另一種方式建立,比如一個wizard)。
Calendar Views(月曆視圖)
月曆視圖為資料提供了時間軸/日程安排的視圖。
View Specification(視圖規範)
這裡是一個視圖例子:
<calendar color="user_id" date_delay="planned_hours" date_start="date_start" string="Tasks">
<field name="name"/>
<field name="project_id"/>
</calendar>
這裡是calendar标簽支援的屬性。
String :視圖的标題字元串
date_start:datetime字段為月曆條目指定開始時間。這個屬性是必須的
date_stop:指定結束日期的datetime。如果date_delay屬性被設定了則忽略這個字段。
date_delay:一個數字記錄來設定一個記錄的時間(機關:小時)。這個屬性将覆寫date-stop并且date_stop将被忽略。
day_length:一個指定工作日長度的整形值。預設是8小時。
color:一個字段,一般是many2one,用來繪制calendar/gantt條目。
mode:設定預設view/zoom模式的字元串值。對于月曆視圖,這個可以是以下值(預設是month):day,week,month
Screenshots(畫面)
月月曆
周月曆
Gantt Views(甘特視圖)
甘特視圖為資料提供了等時線視圖。一般地,其用于顯示項目任務和資源配置。
一個甘特視圖是一個項目中所有任務的圖表顯示。圖上的每一條行是任務計劃完成的時間長度的圖表顯示。
資源任務摘要欄位于所有分組任務之上,代表資源如何高效配置設定在任務中
摘要圖的顔色編碼如下:
- 灰色表示這個資源沒有配置設定任何任務
- 藍色表示這個資源在那個時間配置設定了
- 紅色表示這個資源超額配置設定
View Specification(視圖規範)
下面是一個視圖例子:
<gantt color="user_id" date_delay="planned_hours" date_start="date_start" string="Tasks">
<level object="project.project" link="project_id" domain="[]">
<field name="name"/>
</level>
</gantt>
被gantt标簽接受的屬性與calendar标簽接受的屬性相似。level标簽通過一些many2one字段分組記錄。,目前隻支援一種級别
這裡是一列支援gantt标簽的屬性:
string:視圖的标題字元串
date_start:指定gantt條目開始時間的datetime字段。這個屬性石必選的
date_stop:指定結束日期的datetime字段。如果指定date_delay屬性則忽略
date_delay:指定記錄時間(機關:小時)的numeric字段。此屬性将覆寫date_stop并且忽略它。
day_length:指定工作日長度的整形值,預設是8小時。
color:一個字段,一般是many2one,繪制月曆/甘特圖條目
mode:設定預設的視圖/圖檔模式的字元串值。對于gantt視圖,可以是以下值之一:day,3days,week,3weeks,month,3months,year,3years,5years
level标簽支援以下屬性:
對象
一個openerp對象,與新的視圖有many2one關系
link(連接配接)
連接配接給定對象的目前對象的字段名
domain
用于過濾對象記錄的領域
Drag and Drop(拖曳)
左邊面闆顯示了由給定level字段分組的任務清單。你可以拖曳它們來重新排序或者改變記錄的分組。
The main content pane displays horizontal bars plotted on a timeline grid. A group of bars are summarized with a top summary bar displaying resource allocation of all the underlying tasks.
你可以通過水準拖動任務改變任務的開啟時間。同時通過拖曳一個條形到右邊來改變結束時間。
注意
時間計算要考慮day_length,一個條形可以跨越更多天數,如果一個任務的全部時間比day_length值更大。
Screenshots(畫面)
Design Elements(設計元素)
檔案描述表單的視圖:
<?xml version="1.0"?>
<openerp>
<data>
[view definitions]
</data>
</openerp>
視圖定義主要包含三類标簽:
- 擁有屬性ir.ui.view的<record>标簽,其自身包含了視圖定義
- 屬性為model="ir.actions.act_window"的<record>标簽,連接配接action到這些視圖
- <menuitem>标簽,在菜單中建立選項,并且使用action連接配接
New :你可以在menuitem 标簽中使用分組屬性分組可通路的菜單。
New:可以使用shortcut标簽添加快捷鍵
例子:
<shortcut
name="Draft Purchase Order (Proposals)"
model="purchase.order"
logins="demo"
menu="m"/>
注意你應該在菜單項上添加一個id屬性,其與菜單屬性相關聯。
<record model="ir.ui.view" id="v">
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="priority" eval="2"/>
<field name="arch" type="xml">
<form string="Sale Order">
.........
</form>
</field>
</record>
priority字段的預設值是16,當沒有指定時,系統将使用低一點優先級的視圖。
視圖類型
樹視圖 你可以指定清單中包含的列,還有一些清單的外觀細節。搜尋字段沒有在這裡指定。它們在表單視圖字段的select屬性中指定。
<record id="view_location_tree2" model="ir.ui.view">
<field name="name">stock.location.tree</field>
<field name="model">stock.location</field>
<field name="type">tree</field>
<field name="priority" eval="2"/>
<field name="arch" type="xml">
<tree
colors="blue:usage=='view';darkred:usage=='internal'">
<field name="complete_name"/>
<field name="usage"/>
<field
name="stock_real"
invisible="'product_id' not in context"/>
<field
name="stock_virtual"
invisible="'product_id' not in context"/>
</tree>
</field>
</record>
這個例子隻是一個平的清單,但是了你也可以通過指定 field_parent顯示一個真正的樹形結構。雖然名字有點令人誤解。你指定的字段必須包含所有子條目的清單。
<record id="view_location_tree" model="ir.ui.view">
<field name="name">stock.location.tree</field>
<field name="model">stock.location</field>
<field name="type">tree</field>
<field name="field_parent">child_ids</field>
<field name="arch" type="xml">
<tree toolbar="1">
<field icon="icon" name="name"/>
</tree>
</field>
</record>
在樹元素上,支援以下屬性:
colors 應用不同顔色到清單的條目上。預設是黑色的、
toolbar 如果你想在工具欄區域的頂層顯示樹結構,設定這個值為1。當你點選工具欄的一個條目時,所有其子節點必須在主幹樹中顯示。對于平清單忽略此值。
Grouping Elements(分組元素)
Separator(分隔符)
添加一條分割線
例子:
<separator string="Links" colspan="4"/>
string屬性定義了标簽并且colspan屬性定義了其水準長度(列的數量)
Notebook(筆記本)
<notebook>: 使用筆記本你可以在不同的欄分布視圖字段(每個用一個page标簽定義)你可以設定tabpos屬性設定tab,分别在up,down,left,right 例子:
<notebook colspan="4">....</notebook>
組 <group>:分組成幾列并且根據需要拆分組
- colspan:使用列數
- rowspan:使用行數
- expand : 是否擴充組
- col:提供(給其子節點)的列數
- string : (可選)如果設定了,一個結構将使用包含了字元串的标簽圍在字段分組周圍。否則,其将不可見。
例子:
<group col="3" colspan="2">
<field name="invoiced" select="2"/>
<button colspan="1" name="make_invoice" states="confirmed" string="Make Invoice"
type="object"/>
</group>
Page 定義一個新的筆記本頁面視圖 例子
<page string="Order Line"> ... </page>:
- 定義頁面名字
資料元素
field标簽字段:
- select=“1” 标記這個字段為這個資源查詢視圖的查詢條件之一。1表示基本查詢包含這個字段,并且2表示進階查詢包含這個字段
- colspan=“4”字段必須擴充的列數
- readonly=“1” 設定小部件為隻讀
- required=“1”這個字段為必須的。如果一個字段被标記為必選的,使用者必須填充,如果沒有,系統将不會儲存這個資源。這個屬性替代這個對象中定義的必選值。
- nolable=“1”:隐藏字段的标簽(但在查詢視圖中不隐藏)
- invisible=“True”:隐藏标簽和字段
- password=“true”:用星号“*”替換字段值
- string=“”:改變字段标簽。注意此标簽頁用于查詢視圖:看上面的select屬性
- domain:能限制領域 例子,domain="[('partner_id','=',partner_id)]"
- widget(小部件):能改變小部件 例子:widget="one2many_list" one2one_list,one2many_list,many2one_list,many2many,url,email,image,float_time,reference。
- mode:切換視圖的順序 例子:mode="tree,graph"
- on_change:定義了當字段内容改變時的調用函數 例子:on_change="onchange_partner(type,partner_id)" 詳情看ViewsSpecialProperties
- attrs:允許依靠相同視窗的其他字段定義一個字段的屬性。(它能用于page,group,button,和notebook表)例子:<field digits="(14, 3)" name="volume" attrs="{'readonly':[('type','=','service')]}"/>
- eval:如同Python代碼一樣評估内容(看下面的例子)
- default_focus:當表單第一次打開時設定這個字段的焦點(光标位置)為1。視圖中有這個值為1的屬性可能僅有一個字段
<field name="name" default_focus=”1”/>
Example
這裡是銷售訂單對象視圖的源代碼。
<?xml version="1.0"?>
<openerp>
<data>
<record id="view_partner_form" model="ir.ui.view">
<field name="name">res.partner.form</field>
<field name="model">res.partner</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Partners">
<group colspan="4" col="6">
<field name="name" select="1"/>
<field name="ref" select="1"/>
<field name="customer" select="1"/>
<field domain="[('domain', '=', 'partner')]" name="title"/>
<field name="lang" select="2"/>
<field name="supplier" select="2"/>
</group>
<notebook colspan="4">
<page string="General">
<field colspan="4" mode="form,tree" name="address"
nolabel="1" select="1">
<form string="Partner Contacts">
<field name="name" select="2"/>
<field domain="[('domain', '=', 'contact')]" name="title"/>
<field name="function"/>
<field name="type" select="2"/>
<field name="street" select="2"/>
<field name="street2"/>
<newline/>
<field name="zip" select="2"/>
<field name="city" select="2"/>
<newline/>
<field completion="1" name="country_id" select="2"/>
<field name="state_id" select="2"/>
<newline/>
<field name="phone"/>
<field name="fax"/>
<newline/>
<field name="mobile"/>
<field name="email" select="2" widget="email"/>
</form>
<tree string="Partner Contacts">
<field name="name"/>
<field name="zip"/>
<field name="city"/>
<field name="country_id"/>
<field name="phone"/>
<field name="email"/>
</tree>
</field>
<separator colspan="4" string="Categories"/>
<field colspan="4" name="category_id" nolabel="1" select="2"/>
</page>
<page string="Sales & Purchases">
<separator string="General Information" colspan="4"/>
<field name="user_id" select="2"/>
<field name="active" select="2"/>
<field name="website" widget="url"/>
<field name="date" select="2"/>
<field name="parent_id"/>
<newline/>
</page>
<page string="History">
<field colspan="4" name="events" nolabel="1" widget="one2many_list"/>
</page>
<page string="Notes">
<field colspan="4" name="comment" nolabel="1"/>
</page>
</notebook>
</form>
</field>
</record>
<menuitem
action="action_partner_form"
id="menu_partner_form"
parent="base.menu_base_partner"
sequence="2"/>
</data>
</openerp>
eval屬性
eval屬性如Python代碼一樣評估。允許定義不是字元串的值。
一般地,<field>标簽裡面的值預設為字元串類型
Example 1:
<field name="value">2.3</field>
這個值是字元串“2.3”,而不是浮點型2.3.
Example 2:
<field name="value">False</field>
同理此值是字元串“False”,而不是布爾值False。這裡有點棘手,因為Python的轉換規則将任何非空字元串設定為True,是以上述代碼可能傳回的結果與期望值相反。
如果你想得到float類型的值,一個布爾值或者另外的類型,除了字元串,你需要使用eval屬性:
<field name="value" eval="2.3" />
<field name="value" eval="False" />
Button(按鈕)
給目前視圖添加一個按鈕。允許使用者在目前記錄上執行不同的actions。在點選按鈕後,應該總是重新加載這個記錄。
按鈕有以下屬性:
@type
定義了當按鈕激活的時候執行action的類型
workflow(預設的)
按鈕在使用按鈕的@name(作為工作流名字)的目前模式發送工作流信号并提供記錄id作為參數(在清單中)
工作流信号可能傳回一個action标示符,應該被執行。否則将傳回false。
Object
這個按鈕将執行目前模式@name命名的方法,并提供記錄id作為參數。可能傳回一個action描述符來執行
action
按鈕觸發action(ir.actions.actions)執行。action的id是按鈕的名字。
從這裡開始,下面的是普通的動作執行工作流。
@special
目前僅有一個可能值:cancel,指明彈出應該關閉,不執行任何RPC調用或者action解析。
注意:
隻有在彈出類型視窗才有意義(比如:wizard),否則,等待。
Warning
@special和@type是不能同時使用的。
@name
按鈕标示符,用于訓示調用哪個方法,此方法用來發送信号或者執行action。
@confirm
執行按鈕任務前彈出的一個确認視窗。如果解除了确認,按鈕任務就不會執行。
@string
顯示在按鈕上的标簽
@icon
現在按鈕上的圖示。如果沒有設定,按鈕上顯示的僅僅是文本内容
@states, @attrs, @invisible
标準OpenERP意義的屬性視圖
@default_focus
如果設定值為1,在那個form表單中按下RETURN鍵 自動選擇之前使用的按鈕。
可能會被用戶端忽略
在6.0版本中
<button name="order_confirm" states="draft" string="Confirm Order" icon="gtk-execute"/>
<button name="_action_open_window" string="Open Margins" type="object" default_focus=”1”/>
标簽
使用字元串屬性增加一個簡單标簽作為标題
例子:
<label string="Test"/>
New Line
即使視圖中所有列沒有填充,強制傳回那一行。
例子:
<newline/>
視圖中的繼承
當你在一些普通或者特殊的子產品中建立一個繼承對象時,最好從一個已存在的視圖中繼承(而不是替換)來添加/修改/删除一些字段和儲存。
例子:
<record model="ir.ui.view" id="view_partner_form">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<notebook position="inside">
<page string="Relations">
<field name="relation_ids" colspan="4" nolabel="1"/>
</page>
</notebook>
</field>
</record>
這裡要在基本子產品的res.partner.form視圖的筆記本中添加一個頁面。
繼承引擎将解析存在的視圖并且查詢根節點
<field name="arch" type="xml">
其将添加或者編輯這個标簽的内容。如果這個标簽有一些屬性,其将查找比對這些屬性節點的父視圖。
你将在position屬性上使用這些值:
- inside(預設的):在标簽内部添加值
- after:在标簽後面添加内容
- before:在标簽前面添加值
- replace:替換标簽内容
替換内容:
<record model="ir.ui.view" id="view_partner_form1">
<field name="name">res.partner.form.inherit1</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<page string="Extra Info" position="replace">
<field name="relation_ids" colspan="4" nolabel="1"/>
</page>
</field>
</record>
使用relation_ids字段替換筆記簿中Extra Info欄的内容
父視圖和繼承的視圖使用--update=all 參數更新,如同其他視圖一樣。
内容删除
為從表單中删除一個字段,使用position="replace"屬性的空元素。例如:
<record model="ir.ui.view" id="view_partner_form2">
<field name="name">res.partner.form.inherit2</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="lang" position="replace"/>
</field>
</record>
内容插入
在指定标簽使用position="before"屬性給表單添加元素
<record model="ir.ui.view" id="view_partner_form3">
<field name="name">res.partner.form.inherit3</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="lang" position="before">
<field name="relation_ids"/>
</field>
</field>
</record>
在lang字段前添加relation_ids字段
在指定标簽後給表單添加一個字段,使用position="after"屬性
<record model="ir.ui.view" id="view_partner_form4">
<field name="name">res.partner.form.inherit4</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="lang" position="after">
<field name="relation_ids"/>
</field>
</field>
</record>
在lang字段後面添加relation_ids字段
Multiple Changes(多次變化)
為了多處發生變化,用一個資料元素封裝字段
<record model="ir.ui.view" id="view_partner_form5">
<field name="name">res.partner.form.inherit5</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<data>
<field name="lang" position="replace"/>
<field name="website" position="after">
<field name="lang"/>
</field>
</data>
</field>
</record>
從lang的普通位置删除lang字段,并在website字段後顯示
XPath Element
有時候一個視圖通過名字簡單定義一個定義一個target字段太複雜。例如,這個字段可能位于兩個地方。當處理這種情況時,你可使用xpath元素來描述變化發生的地方。
<record model="ir.ui.view" id="view_partner_form6">
<field name="name">res.partner.form.inherit6</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<data>
<xpath
expr="//field[@name='address']/form/field[@name='email']"
position="after">
<field name="age"/>
</xpath>
<xpath
expr="//field[@name='address']/tree/field[@name='email']"
position="after">
<field name="age"/>
</xpath>
</data>
</field>
</record>
在位址清單的表單和樹視圖email字段後面添加age字段
指定你想使用的視圖
有時候你想指定一個視圖而不是用預設的
- 如果一個對象對應着幾個表單視圖或者樹視圖
- 如果你想通過關聯id來改變使用的表單或者樹視圖(one2many例子)
使用priority字段
在視圖定義中,這個字段是可用的,預設值為16.一般地,OpenERP将顯示使用最高優先級的模式(最小值)。例如,假設對于一個模式我們有兩個視圖。模式用戶端有兩個字段:firstname和lastname。我們将定義兩個視圖,一個顯示首先firstname,另一個首先顯示lastName。
<!--
Here is the first view for the model 'client'.
We don't specify a priority field, which means
by default 16.
-->
<record model="ir.ui.view" id="client_form_view_1">
<field name="name">client.form.view1</field>
<field name="model">client</field>
<field name="type">form</fiel>
<field name="arch" type="xml">
<field name="firstname"/>
<field name="lastname"/>
</field>
</record>
<!--
A second view, which show fields in an other order.
We specify a priority of 15.
-->
<record model="ir.ui.view" id="client_form_view_2">
<field name="name">client.form.view2</field>
<field name="model">client</field>
<field name="priority" eval="15"/>
<field name="type">form</fiel>
<field name="arch" type="xml">
<field name="lastname"/>
<field name="firstname"/>
</field>
</record>
閑雜,每次OpenERP得為獨享client顯示一個表單視圖,在兩個視圖之間有一個選擇。其将總是使用第二個視圖,因為有高點的優先級。除非你告訴它使用第一個。
Specify per-action view
為說明這點,我們将建立兩個表單項來顯示client對象的表單視圖
<!--
This action open the default view (in our case,
the view with the highest priority, the second one)
-->
<record
model="ir.actions.act_window"
id="client_form_action">
<field name="name">client.form.action</field>
<field name="res_model">client</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
</record>
<!--
This action open the view we specify.
-->
<record
model="ir.actions.act_window"
id="client_form_action1">
<field name="name">client.form.action1</field>
<field name="res_model">client</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="client_form_view_1"/>
</record>
<menuitem id="menu_id" name="Client main menu"/>
<menuitem
id="menu_id_1"
name="Here we don't specify the view"
action="client_form_action" parent="menu_id"/>
<menuitem
id="menu_id_1"
name="Here we specify the view"
action="client_form_action1" parent="menu_id"/>
如你所見,我們可以指定一個視圖。這意味着當我們打開第二個菜單時,OpenERP将使用表單視圖client_form_view_1,而不僅僅取決于優先級。
注意
如果你要關聯一個在其他子產品定義的視圖,記得使用ref屬性的子產品名(module.view_id)
指定關聯字段的視圖
使用上下文
雖然view_id在菜單/action中工作的很好,但是如何指定一個視圖使用one2many字段?當你有一個one2many字段時,使用兩個視圖。一個樹視圖(藍色),和當你點選紅色按鈕時顯示的表單視圖
當你在一個表單視圖中添加一個one2many字段,你應該做這些:
<field name="order_line" colspan="4" nolabel="1"/>
如果你想指定要使用的視圖,你可以添加一個上下文屬性,并為每類支援的視圖指定一個視圖id,如同action中的view_id屬性一樣,除了提供的視圖id必須是全限定子產品名,甚至是必須屬于同一個子產品。
注意
你得将子產品名放在view_id中,因為當視圖顯示的時候已經評估,并且當XML檔案解析時沒有評估,是以了子產品名資訊不可用。結果是将使用預設的視圖(看下面的)
如果你沒有指定視圖,OpenERP将按照以下順序選擇一個:
- 将使用定義在字段中的<form>或者<tree>視圖
- 或者,使用這個對象優先級最高的視圖
- 最後,建立預設的空視圖,含有所有字段
注意
上下文關鍵字命名為<視圖類型>_view_ref
注意
預設地,OpenERP從不使用不是為你的對象定義的視圖。如果你有兩個子產品,有相同的字段,但是了有不同的model名字,OpenERP将從不使用其中一個的視圖,甚至是一個模型繼承了另一個模型
你可以強制性手動控制一個視圖,要不在action或者在上下文中。
使用子視圖
在關聯字段的情況下,你可以直接在一個字段裡面直接建立一個視圖。
<record model="ir.ui.view" id="some_view">
<field name="name">some.view</field>
<field name="type">form</field>
<field name="model">some.model.with.one2many</field>
<field name="arch" type="xml">
<field name="..."/>
<!-- <=== order_line is a one2many field -->
<field name="order_line" colspan="4" nolabel="1">
<form>
<field name="qty"/>
...
</form>
<tree>
<field name="qty"/>
...
</tree>
</field>
</field>
如果開發人員想在另一個子產品中繼承這個視圖,你需要繼承其父視圖并且修改其子字段。在子視圖中,你通常需要使用XPath Element來确切描述在哪裡放置你的新字段。
<record model="ir.ui.view" id="some_inherited_view">
<field name="name">some.inherited.view</field>
<field name="type">form</field>
<field name="model">some.model.with.one2many</field>
<field name="inherit_id" ref="core_module.some_view"/>
<field name="arch" type="xml">
<data>
<xpath
expr="//field[@name='order_line']/form/field[@name='qty']"
position="after">
<field name="size"/>
</xpath>
<xpath
expr="//field[@name='order_line']/tree/field[@name='qty']"
position="after">
<field name="size"/>
</xpath>
</data>
</field>
像這樣定義子視圖不利的一面是其不能繼承自身,其僅能被其父視圖繼承。你的視圖将更靈活 如果分開定義子視圖并之後指定哪個子視圖定義 one2many 字段部分。