在上一章中已經看到,odoo能夠為給定模型生成默認視圖,實際上,默認視圖對于業務應用程式來說是不可接受的,相反,我們至少應該以邏輯的方式組織各個欄位,
視圖是在帶有操作和選單的XML檔案中定義的,它們是ir.ui.view model的實體,
在我們的estate模塊中,我們需要以邏輯方式組織欄位:
- 在串列(樹)視圖中,我們希望顯示的不僅僅是名稱,
- 在表單視圖中,應該對欄位進行分組,
- 在搜索視圖中,我們必須能夠搜索的不僅僅是名稱,具體來說,我們需要"Available"的地產篩選器和按"postcode"分組的快捷方式
List(串列)
參考: 主題關聯檔案可參考List.
串列視圖,也叫樹(tree)視圖, 以表格的形式顯示記錄,
視圖根元素為<tree>,其最基礎版本僅簡單的列出要在表中顯示的所有欄位(其中每個欄位都是一列):
<tree string="Tests">
<field name="name"/>
<field name="last_seen"/>
</tree>
練習 -- 添加一個自定義串列視圖
在合適的XML檔案中為estate.property model定義一個串列視圖, 一個簡單的示例
修改odoo14/custom/estate/views/estate_property_views.xml
<?xml version="1.0"?>
<odoo>
<record id="link_estate_property_action" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
</record>
<!--本小節添加的內容-->
<record id="estate_property_view_tree" model="ir.ui.view">
<field name="name">estate.property.tree</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<tree string="Tests">
<field name="name" string="Title"/>
<field name="postcode" string="Postcode"/>
<field name="bedrooms" string="Bedrooms"/>
<field name="living_area" string="Living Area"/>
<field name="expected_price" string="Expected Price"/>
<field name="selling_price" string="Selling Price"/>
<field name="date_availability" string="Avalilable Form"/>
</tree>
</field>
</record>
</odoo>
注意:暫時不要添加示例中的 editable="bottom"屬性
說明:
<field name="name">自定義串列名稱</field>
<field name="model">模型名稱,即_name的值</field>
重啟服務,瀏覽器驗證,效果如下:

說明:如果未給<field/>添加string屬性,則顯示如下:

Form(表單)
參考: 主題關聯檔案可以查看Form.
表單用于創建和編輯單條件記錄,其根元素為 <form>,由高層框架元素(group和notebook)和互動元素 (按鈕和欄位):
<form string="Test">
<sheet>
<group>
<group>
<field name="name"/>
</group>
<group>
<field name="last_seen"/>
</group>
<notebook>
<page string="Description">
<field name="description"/>
</page>
</notebook>
</group>
</sheet>
</form>
可以使用常規HTML標記(如"div"和"h1")以及"class"屬性(Odoo提供了一些內置類)來微調外觀,
一個簡單示例.
練習 -- 添加自定義表單視圖
在合適的XML檔案中為estate.property 定義視圖
為了避免每次修改視圖時都重新啟動服務器,可以在啟動服務器時添加--dev-xml,以便只重繪頁面就可以查看視圖修改,如下:
python odoo-bin --addons-path=custom,odoo/addons -r myodoo -w test123 -d odoo -u estate --dev xml
修改odoo14/custom/estate/views/estate_property_views.xml
<?xml version="1.0"?>
<odoo>
<record id="link_estate_property_action" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
</record>
<record id="action_lost_leads" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
</record>
<record id="estate_property_view_tree" model="ir.ui.view">
<field name="name">estate.property.tree</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<tree string="Tests">
<field name="name" string="Title"/>
<field name="postcode" string="Postcode"/>
<field name="bedrooms" string="Bedrooms"/>
<field name="living_area" string="Living Area"/>
<field name="expected_price" string="Expected Price"/>
<field name="selling_price" string="Selling Price"/>
<field name="date_availability" string="Avalilable Form"/>
</tree>
</field>
</record>
<!--本小節添加的內容-->
<record id="estate_property_view_form" model="ir.ui.view">
<field name="name">estate.property.form</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<form string="estate property form">
<sheet>
<h1>
<field name="name"/>
</h1>
<group>
<group>
<field name="postcode" string="Postcode" ></field>
<field name="date_availability" string="Available From"></field>
</group>
<group>
<field name="expected_price" string="Expected Price"></field>
<field name="selling_price" string="Selling Price"></field>
</group>
</group>
<notebook>
<page string="Description">
<group>
<field name="description"></field>
<field name="bedrooms"></field>
<field name="living_area"></field>
<field name="facades"></field>
<field name="garage"></field>
<field name="garden"></field>
<field name="garden_area"></field>
<field name="garden_orientation"></field>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</odoo>
查看效果

Search(搜索)
參考: 本主題相關檔案可參考Search.
搜索視圖與串列及表單視圖略有不同,因為它們不顯示內容,盡管它們適用于特定模型,但它們用于過濾其他視圖的內容(通常是聚合視圖,比如串列). 除了在使用方面的不同,他們的定義方式是一樣的,
搜索視圖根元素為<search>,該視圖最基礎的版本是列出需要快捷方式的所有欄位:
<search string="Tests">
<field name="name"/>
<field name="last_seen"/>
</search>
Odoo生成的默認搜索視圖提供了按name篩選的快捷方式,在自定義搜索視圖中添加用戶可能過濾的欄位是非常常見的,
搜索視圖還可以包含<filter>元素,這些元素充當預定義搜索的開關,篩選器必須具有以下屬性之一:
domain:將給定domain添加到當前搜索dontext:添加一些context到當前搜索,使用group_by按給定欄位名稱對結果分組,
一個簡單的示例.
<record id="view_delivery_carrier_search" model="ir.ui.view">
<field name="name">delivery.carrier.search</field>
<field name="model">delivery.carrier</field>
<field name="arch" type="xml">
<search string="Delivery Carrier">
<field name="name" string="Carrier" />
<field name="delivery_type"/>
<separator/>
<filter string="Archived" name="inactive" domain="[('active', '=', False)]"/>
<group expand="1" string="Group By">
<filter string="Provider" name="provider" context="{'group_by':'delivery_type', 'residual_visible':True}"/>
</group>
</search>
</field>
</record>
在進一步練習之前,有必要介紹一下domain概念,
domain
參考: 本主題相關檔案可參考 Search domains.
在odoo中,domain對記錄上的條件進行編碼:domain是用于選擇模型記錄子集的條件串列,每個條件都是一個包含欄位名、運算子和值的三元組,如果指定欄位滿足作用于值的運算子的條件,則記錄滿足條件,
例如,當在Product模型上使用時,以下domain選擇單價高于1000的所有services:
[('product_type', '=', 'service'), ('unit_price', '>', 1000)]
默認情況下,條件與隱式AND組合在一起,這意味著記錄匹配一個domain,需要滿足domain中的每個條件,邏輯運算子&(AND)、|(OR)和!(NOT)可用于顯式組合條件,它們用于前綴位置(運算子插入在其引數之前,而不是插入在引數之間),例如,選擇型別為“服務“或“單價”不介于1000和2000之間的產品
['|',
('product_type', '=', 'service'),
'!', '&',
('unit_price', '>=', 1000),
('unit_price', '<', 2000)]
選擇型別為“服務“且“單價”介于1000和2000之間的產品
['&',('product_type', '=', 'service'),'&',('unit_price', '>=', 1000),('unit_price', '<', 2000)]
等價于
[('product_type', '=', 'service'),('unit_price', '>=', 1000),('unit_price', '<', 2000)]
選擇型別為“服務“或者“單價”大于等于1000或者單價小于500的產品
['|', '|', ('product_type', '=', 'service'),('unit_price', '>=', 1000),('unit_price', '<', 500)]
選擇名字為 ABC 而且語言編碼不為 en_US 而且國家的編碼為 be 或者 de
[('name','=','ABC'),
('language.code','!=','en_US'),
'|',('country_id.code','=','be'),
('country_id.code','=','de')]
簡單一點的寫法
[('name','=','ABC'),
('language.code','!=','en_US'),
('country_id.code','in', ['be', 'de'])]
波蘭表示法簡介
Odoo是使用了波蘭表示法,簡單來說,波蘭表示法是一種運算子置于運算元前,并且不需要括號仍然能無歧義地決議表達的方法,
運算順序
以二元運算為例,從左至右讀入運算式,遇到一個運算子后跟隨兩個運算元時,則計算之,然后將結果作為運算元替換這個運算子和兩個運算元;重復此步驟,直至所有運算子處理完畢,
舉個例子:
['|','&','|',a,b,c,'&',d,e]
其中a,b,c,e,f,g 分別是不帶邏輯運算子的運算式,運算式的運算順序:
1、['|','&','|',a,b,c,'&',d,e]
2、['|','&',(a | b),c,'&',d,e]
3、['|',((a | b) & c),'&',d,e]
4、['|',((a | b) & c),(d & e)]
5、[(((a | b) | c) | (d & e))]
如果我們要做到這個效果
A and (B or C) and D and E
先從里面開始,把or提前
A and (or B C) and D and E
把里面的and提前,去掉括號
and A or B C and D E
所以最后的domain可以這樣寫
A, '|', B, C, D, E
當然,我們也可以把運算式寫得更容易看一點,如下:
A, D, E, '|', B, C
練習
添加定義搜索視圖
在合適的XML中為 estate.property 模型定義一個搜索視圖
添加過濾和分組
添加以下內容到之前創建就的搜索視圖
- 一個顯示avaliable地產的過濾器,也就說,state應該為 “New“ 或者“Offer Received”,
- 按"postcode"分組的能力
修改odoo14/custom/estate/views/estate_property_views.xml
<?xml version="1.0"?>
<odoo>
<record id="link_estate_property_action" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
</record>
<record id="action_lost_leads" model="ir.actions.act_window">
<field name="name">Properties</field>
<field name="res_model">estate.property</field>
<field name="view_mode">tree,form</field>
</record>
<record id="estate_property_view_tree" model="ir.ui.view">
<field name="name">estate.property.tree</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<tree string="Tests">
<field name="name" string="Title"/>
<field name="postcode" string="Postcode"/>
<field name="bedrooms" string="Bedrooms"/>
<field name="living_area" string="Living Area"/>
<field name="expected_price" string="Expected Price"/>
<field name="selling_price" string="Selling Price"/>
<field name="date_availability" string="Avalilable Form"/>
</tree>
</field>
</record>
<record id="estate_property_view_form" model="ir.ui.view">
<field name="name">estate.property.form</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<form string="estate property form">
<sheet>
<h1>
<field name="name"/>
</h1>
<group>
<group>
<field name="postcode" string="Postcode" ></field>
<field name="date_availability" string="Available From"></field>
</group>
<group>
<field name="expected_price" string="Expected Price"></field>
<field name="selling_price" string="Selling Price"></field>
</group>
</group>
<notebook>
<page string="Description">
<group>
<field name="description"></field>
<field name="bedrooms"></field>
<field name="living_area"></field>
<field name="facades"></field>
<field name="garage"></field>
<field name="garden"></field>
<field name="garden_area"></field>
<field name="garden_orientation"></field>
</group>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<!--本小節添加的內容-->
<record id="estate_property_search_view" model="ir.ui.view">
<field name="name">estate.property.search</field>
<field name="model">estate.property</field>
<field name="arch" type="xml">
<search string="Estate Property">
<!-- 搜索 -->
<field name="name" string="Title" />
<field name="postcode" string="Postcode"></field>
<separator/>
<!-- 篩選 -->
<filter string="Available" name="state" domain="['|',('state', '=', 'New'),('state', '=', 'Offer Received')]"></filter>
<filter name="bedrooms" domain="[('bedrooms', '>', 3)]"></filter>
<filter name="bedrooms and selling_price" domain="[('bedrooms', '>', 2),('selling_price', '>=', 1000)]"></filter>
<!-- 分組 -->
<group expand="1" string="Group By">
<filter string="朝向" name="garden_orientation" context="{'group_by':'garden_orientation'}"/>
</group>
</search>
</field>
</record>
</odoo>
重啟服務,驗證效果

作者:授客
微信/QQ:1033553122
全國軟體測驗QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于時間倉促,文中可能存在錯誤,歡迎指正、評論!
作者五行缺錢,如果覺得文章對您有幫助,請掃描下邊的二維碼打賞作者,金額隨意,您的支持將是我繼續創作的源動力,打賞后如有任何疑問,請聯系我!!!
微信打賞
支付寶打賞 全國軟體測驗交流QQ群
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548207.html
標籤:其他
