基本視圖
視圖定義了模型數據的呈現方式。不同的視圖類型決定了數據的可視化方式(記錄行列表、圖形化聚合)。視圖可以通過類型(比如partners列表)或id被請求。對于一般請求,將被對應類型的最低優先級視圖響應(每個類型的最低優先級視圖是該類型的默認視圖)。視圖繼承允許更改在其他地方聲明的視圖(添加或刪除內容)。
通用視圖聲明
視圖通過一個ir.ui.view
的模型記錄來聲明。視圖類型由arch
字段的根元素隱含定義:
<record model="ir.ui.view" id="view_id">
<field name="name">view.name</field>
<field name="model">object_name</field>
<field name="priority" eval="16"/>
<field name="arch" type="xml">
<!-- view content: <form>, <tree>, <graph>, ... -->
</field>
</record>
警告
因為視圖的內容是XML,所以arch
字段必須被聲明為type="xml"
以正確解析。
樹視圖
樹視圖也被稱為列表視圖,以表格形式顯示記錄。根元素是<tree>
.最簡單的樹視圖是在表格中列出所有字段(每列對應一個字段):
<tree string="Idea list">
<field name="name"/>
<field name="inventor_id"/>
</tree>
表單視圖
表單視圖通常用來建立和編輯單條記錄。根元素是<form>
,由結構元素(groups,notebooks)和交互元素(button,fields)組成。
<form string="Idea form">
<group colspan="4">
<group colspan="2" col="2">
<separator string="General stuff" colspan="2"/>
<field name="name"/>
<field name="inventor_id"/>
</group>
<group colspan="2" col="2">
<separator string="Dates" colspan="2"/>
<field name="active"/>
<field name="invent_date" readonly="1"/>
</group>
<notebook colspan="4">
<page string="Description">
<field name="description" nolabel="1"/>
</page>
</notebook>
<field name="state"/>
</group>
</form>
練習使用XML定制窗體視圖
建立課程對象的表單視圖,顯示課程的名稱和描述字段。
openacademy/views/openacademy.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record model="ir.ui.view" id="course_form_view">
<field name="name">course.form</field>
<field name="model">openacademy.course</field>
<field name="arch" type="xml">
<form string="Course Form">
<sheet>
<group>
<field name="name"/>
<field name="description"/>
</group>
</sheet>
</form>
</field>
</record>
<!-- window action -->
<!--
The following tag is an action definition for a "window action",
練習notebook結構元素
在課程的表單視圖中,將描述字段放在一個選項卡中,然后再添加選項卡放置其它字段。修改后的課程表單視圖如下:
openacademy/views/openacademy.xml
<sheet>
<group>
<field name="name"/>
</group>
<notebook>
<page string="Description">
<field name="description"/>
</page>
<page string="About">
This is an example of notebooks
</page>
</notebook>
</sheet>
</form>
</field>
表單視圖也可以使用純HTML來進行更靈活的布局
<form string="Idea Form">
<header>
<button string="Confirm" type="object" name="action_confirm"
states="draft" class="oe_highlight" />
<button string="Mark as done" type="object" name="action_done"
states="confirmed" class="oe_highlight"/>
<button string="Reset to draft" type="object" name="action_draft"
states="confirmed,done" />
<field name="state" widget="statusbar"/>
</header>
<sheet>
<div class="oe_title">
<label for="name" class="oe_edit_only" string="Idea Name" />
<h1><field name="name" /></h1>
</div>
<separator string="General" colspan="2" />
<group colspan="2" col="2">
<field name="description" placeholder="Idea description..." />
</group>
</sheet>
</form>
搜索視圖
搜索視圖可對列表視圖(或者其它聚合視圖)中的字段進行搜索。搜索視圖的根元素是<search>
,內容包含所有可以搜索的字段。
<search>
<field name="name"/>
<field name="inventor_id"/>
</search>
如果在模型中沒有定義搜索視圖,Odoo會生成一個只包含name
字段的搜索視圖。
練習搜索課程
通過標題和描述來搜索課程。
openacademy/views/openacademy.xml
</field>
</record>
<record model="ir.ui.view" id="course_search_view">
<field name="name">course.search</field>
<field name="model">openacademy.course</field>
<field name="arch" type="xml">
<search>
<field name="name"/>
<field name="description"/>
</search>
</field>
</record>
<!-- window action -->
<!--
The following tag is an action definition for a "window action",