View records

视图定义了记录应如何展示给最终用户。它们以 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

报告、网站等的模板化…

Graph

可视化对多条记录或记录组的聚合。

Pivot

将聚合显示为 数据透视表

日历

将记录显示为每日、每周、每月或每年的日历事件。

Cohort Enterprise feature

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

甘特图 Enterprise feature

将记录显示为甘特图。

Grid 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.

Requirement

可选的

类型

Char

model

The model linked to the view, if applicable.

Requirement

强制性的

类型

Char

arch

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

Requirement

可选的

类型

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.

Requirement

可选的

类型

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.

Requirement

可选的

类型

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.

Requirement

可选的

类型

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.

Requirement

可选的

类型

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 属性指定了 继承位置

视图解析

分辨率生成最终 arch 用于请求/匹配的 primary 视图,如下所示:

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

  2. if the view has no parent, its arch is used as-is;

  3. 当前视图的子视图如果模式为 extension,则会进行查找,并且它们的继承规范会以深度优先的方式应用(先应用子视图,然后是其子视图,接着是其兄弟视图)。

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

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

继承规范

继承规范按顺序应用,并包含以下内容:

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

  2. children element 用于修改继承的元素。

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

  • 一个带有 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 属性或两者兼具,则根据 addremove 的值以及默认为 , 的可选 separator 属性,重新计算匹配节点以 name 命名的属性值。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>

Model commons

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