查看记录

视图定义了记录应如何显示给最终用户。它们以 XML 的形式指定,并作为记录本身存储,这意味着它们可以独立于所表示的模型进行编辑。视图具有灵活性,允许对它们所控制的屏幕进行高度定制。存在多种 视图类型。每种视图代表一种可视化模式:表单列表看板 等。

通用结构

基本视图通常共享下面定义的公共最小结构。占位符以全大写字母表示。

<record id="ADDON.MODEL_view_TYPE" model="ir.ui.view">
  <field name="name">NAME</field>
  <field name="model">MODEL</field>
  <field name="arch" type="xml">
    <VIEW_TYPE>
      <views/>
    </VIEW_TYPE>
  </field>
</record>

视图类型

表单

显示并编辑单条记录的数据。

列表

查看和编辑多个记录。

搜索

应用筛选条件并执行搜索。结果将在当前列表、看板…视图中显示。

看板

以“卡片”形式显示记录,可作为小型模板进行配置。

Qweb

报告模板、网站…

图表

可视化多个记录或记录组的汇总信息。

透视表

透视表 显示聚合数据。

日历

按日历形式显示记录,支持每日、每周、每月或每年的视图。

群体 Enterprise feature

显示并理解某些数据在一段时间内的变化情况。

甘特图 |企业版|

以甘特图显示记录。

网格 Enterprise feature

在数值单元格中显示计算信息;几乎无法配置。

映射 Enterprise feature

在地图上显示记录,并显示它们之间的路线。

字段

视图记录显示多个字段。

class odoo.addons.base.models.ir_ui_view.View[源代码]
name

Only useful as a mnemonic/description of the view when looking for one in a list of some sort. Most Odoo view names start with the name of the addon and end with the type of view being discussed.

要求

可选

类型

Char

model

The model linked to the view, if applicable.

要求

必填

类型

Char

arch

The description of the view layout depending on the view type.

要求

可选

类型

Text

groups_id

The groups allowed to use/access the current view.

If the view extends an existing view, the extension will be applied only for a given user, if that user has access to the provided groups_id.

要求

可选

类型

Many2many -> Groups

priority

When requesting a view by specifying the model and type, the matching view with the lowest priority is returned (it is the default view).

It also defines the order of views application during view resolution. When a view is requested by id and its mode is not primary, its closest parent with mode = primary is matched.

要求

可选

类型

Integer

inherit_id

Reference to the parent view on which the inheritance will be applied. Its value is used by default. Specify the parent using the ref attribute with ref="ADDON.MODEL_parent_view_TYPE".

The addon name (before the dot) is not necessary if the inheritance is done on a record of the same module.

See 继承 for more information.

要求

可选

类型

Many2one

mode

Only applies if this view inherits from an other one (inherit_id is set).

extension

If the view is requested, the closest primary view is looked up (via inherit_id). Then, all views inheriting from it with this view’s model are applied.

primary

The closest primary view is fully resolved (even if it uses a different model than the current one). Then, the view’s inheritance specs are applied, and the result is used as if it were this view’s actual arch.

A case in which one would want to override mode while using inherit_id is delegation inheritance. In that case, your derived model is separated from its parent, and views matching with one won’t match with the other. Assuming one inherits from a view associated with the parent model and wants to customize the derived view to show data from the derived model, the mode of the derived view needs to be set to primary because it is the base (and maybe only) view for that derived model. Otherwise, the view matching rules won’t apply.

See 继承 for more information.

要求

可选

类型

Selection: extension / primary

默认

extension

注解

当前上下文和用户访问权限也可能影响视图的可用性。

继承

继承允许对已交付的视图进行自定义。例如,可以实现在安装模块时添加内容,或根据不同的动作提供不同的显示效果。

继承的视图通常遵循下面定义的通用结构。占位符以全大写字母表示。此合成视图将更新由 XPath 定位的节点,以及由名称和属性定位的另一个节点。

<record id="ADDON.MODEL_view_TYPE" model="ir.ui.view">
    <field name="model">MODEL</field>
    <field name="inherit_id" ref="VIEW_REFERENCE"/>
    <field name="mode">MODE</field>
    <field name="arch" type="xml">
        <xpath expr="XPATH" position="POSITION">
            <CONTENT/>
        </xpath>
        <NODE ATTRIBUTES="VALUES" position="POSITION">
            <CONTENT/>
        </NODE>
    </field>
</record>

inherit_idmode 字段决定了 视图解析xpathNODE 元素指定了 继承规范exprposition 属性指定了 继承位置

视图解析

解析生成请求/匹配的 primary 视图的最终 arch,如下所示:

  1. 如果视图有父视图,则父视图会被完全解析,然后应用当前视图的继承规范;

  2. 如果视图没有父视图,其 arch 将直接使用;

  3. 当前视图的子视图中,模式为 extension 的部分会被查找,并按照深度优先的方式应用其继承规范(先应用子视图,然后是子视图的子视图,最后是子视图的兄弟视图)。

继承是根据 inherit_id 字段应用的。如果多个视图记录继承同一个视图,则顺序由 priority 确定。

应用子视图后的结果生成最终的 arch

继承规范

继承规范是按顺序应用的,包括:

  1. 一个元素定位器,用于匹配父视图中的继承元素;

  2. 子元素,用于修改继承的元素。

有三种类型的元素定位器:

  • 一个带有 expr 属性的 xpath 元素。expr 是一个应用于当前 archXPath 表达式 1,用于匹配它找到的第一个节点;

  • 一个带有 name 属性的 field 元素,匹配第一个具有相同 name 的字段。

    注解

    所有其他属性都会被忽略。

  • 任何其他元素,与第一个具有相同 name 和相同属性的元素匹配。

    注解

    positionversion 属性被忽略。

1

为 QWeb 视图中更简单的匹配添加了一个扩展函数:hasclass(*classes),如果上下文节点包含所有指定的类,则匹配。

Example

<xpath expr="page[@name='pg']/group[@name='gp']/field" position="inside">
    <field name="description"/>
</xpath>

<div name="name" position="replace">
    <field name="name2"/>
</div>

继承位置

继承规范接受一个可选的 position 属性,默认值为 inside,用于指定如何修改匹配的节点。

inside

继承规范的内容将附加到匹配的节点上。

Example

<notebook position="inside">
    <page string="New feature">
        ...
    </page>
</notebook>
after

继承规范的内容会在匹配节点的父节点中,于匹配节点之后追加。

Example

<xpath expr="//field[@name='x_field']" position="after">
    <field name="x_other_field"/>
</xpath>
before

继承规范的内容会在匹配节点的父节点中追加到该节点之前。

Example

<field name=x_field" position="before">
    <field name="x_other_field"/>
</field>
replace

继承规范的内容会替换匹配的节点。在规范内容中,任何仅包含 $0 的文本节点都会被替换为匹配节点的副本,从而实现对匹配节点的包裹效果。

Example

<xpath expr="//field[@name='x_field']" position="replace">
    <div class="wrapper">
        $0
    </div>
</xpath>
attributes

继承规范的内容应仅由 attribute 元素组成,每个元素都具有一个 name 属性和一个可选的内容。

  • 如果 attribute 元素包含正文,则会在匹配的节点上添加一个名称为其 name 的属性,其值为 attribute 元素的文本。

  • 如果 attribute 元素没有内容,则从匹配的节点中移除以其 name 为名称的属性。

  • 如果 attribute 元素具有 add 属性、remove 属性,或两者都有,则会重新计算匹配节点上名为 name 的属性值,以考虑 addremove 的值以及一个可选的 separator 属性(默认为 ,)。add 包含其值,由 separator 分隔。remove 会移除其值,由 separator 分隔。

Example

<field name="x_field" position="attributes">
    <attribute name="invisible">True</attribute>
    <attribute name="class" add="mt-1 mb-1" remove="mt-2 mb-2" separator=" "/>
</field>
move

在继承规范的内容上设置属性 position="move",以指定节点相对于继承规范的元素定位器的位置,该属性 position 必须也设置在继承规范的元素定位器上,其值可以是 insidereplaceafterbefore

Example

<xpath expr="//@target" position="after">
    <xpath expr="//@node" position="move"/>
</xpath>

<field name="target_field" position="after">
    <field name="my_field" position="move"/>
</field>

模型 公共组件

class odoo.addons.base.models.ir_ui_view.View[源代码]
Model.get_views(views, options=None)

Returns the fields_views of given views, along with the fields of the current model, and optionally its filters for the given action.

The return of the method can only depend on the requested view types, access rights (views or other records), view access rules, options, context lang and TYPE_view_ref (other context values cannot be used).

Python expressions contained in views or representing domains (on python fields) will be evaluated by the client with all the context values as well as the record values it has.

参数
  • views – list of [view_id, view_type]

  • options (dict) –

    a dict optional boolean flags, set to enable:

    toolbar

    includes contextual actions when loading fields_views

    load_filters

    returns the model’s filters

    action_id

    id of the action to get the filters, otherwise loads the global filters or the model

返回

dictionary with fields_views, fields and optionally filters

Model.get_view([view_id | view_type='form'])

Get the detailed composition of the requested view like model, view architecture.

The return of the method can only depend on the requested view types, access rights (views or other records), view access rules, options, context lang and TYPE_view_ref (other context values cannot be used).

参数
  • view_id (int) – id of the view or None

  • view_type (str) – type of the view to return if view_id is None (‘form’, ‘list’, …)

  • options (dict) – boolean options to return additional features: - bool mobile: true if the web client is currently using the responsive mobile view (to use kanban views instead of list views for x2many fields)

返回

composition of the requested view (including inherited views and extensions)

返回类型

dict

引发
  • AttributeError

    • if the inherited view has unknown position to work with other than ‘before’, ‘after’, ‘inside’, ‘replace’

    • if some tag other than ‘position’ is found in parent view

  • Invalid ArchitectureError – if there is view type other than form, list, calendar, search etc… defined on the structure