莊將椿 发表于 2023-3-26 22:04:21

odoo 开发入门教程系列-基本视图

在上一章中已经看到,odoo能够为给定模型生成默认视图。实际上,默认视图对于业务应用程序来说是不可接受的。相反,我们至少应该以逻辑的方式组织各个字段。
视图是在带有操作和菜单的XML文件中定义的。它们是ir.ui.view model的实例。
在我们的estate模块中,我们需要以逻辑方式组织字段:

[*]在列表(树)视图中,我们希望显示的不仅仅是名称。
[*]在表单视图中,应该对字段进行分组。
[*]在搜索视图中,我们必须能够搜索的不仅仅是名称。具体来说,我们需要"Available"的地产筛选器和按"postcode"分组的快捷方式
List(列表)

参考: 主题关联文档可参考List.
列表视图,也叫树(tree)视图, 以表格的形式显示记录。
视图根元素为。其最基础版本仅简单的列出要在表中显示的所有字段(其中每个字段都是一列):
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>练习 -- 添加一个自定义列表视图
在合适的XML文件中为estate.property model定义一个列表视图。 一个简单的示例
修改odoo14/custom/estate/views/estate_property_views.xml
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    Properties<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>tree,form<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property.tree<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    注意:暂时不要添加示例中的 editable="bottom"属性
说明:
<field name="name">自定义列表名称</field>
<field name="model">模型名称,即_name的值</field>重启服务,浏览器验证,效果如下:

说明:如果未给添加string属性,则显示如下:

Form(表单)

参考: 主题关联文档可以查看Form.
表单用于创建和编辑单条件记录,其根元素为 ,由高层框架元素(group和notebook)和交互元素 (按钮和字段):
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    可以使用常规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
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    Properties<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>tree,form<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>Properties<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>tree,form<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property.tree<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property.form<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>estate.property<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>   

<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>查看效果

Search(搜索)

参考: 本主题相关文档可参考Search.
搜索视图与列表及表单视图略有不同,因为它们不显示内容。尽管它们适用于特定模型,但它们用于过滤其他视图的内容(通常是聚合视图,比如列表). 除了在使用方面的不同,他们的定义方式是一样的。
搜索视图根元素为。该视图最基础的版本是列出需要快捷方式的所有字段:
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>Odoo生成的默认搜索视图提供了按name筛选的快捷方式。在自定义搜索视图中添加用户可能过滤的字段是非常常见的。
搜索视图还可以包含元素,这些元素充当预定义搜索的开关。筛选器必须具有以下属性之一:

[*]domain:将给定domain添加到当前搜索
[*]dontext:添加一些context到当前搜索,使用group_by按给定字段名称对结果分组。
一个简单的示例.
<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    delivery.carrier.search<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    delivery.carrier<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>在进一步练习之前,有必要介绍一下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'),    '!', '&',<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>('unit_price', '>=', 1000),<tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>('unit_price', '=', 1000),('unit_price', '=', 1000),('unit_price', '=', 1000),('unit_price', '', 3)]"><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree><tree string="Tests">
    <field name="name"/>
    <field name="last_seen"/>
</tree>    重启服务,验证效果


来源:https://www.cnblogs.com/shouke/p/17253327.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: odoo 开发入门教程系列-基本视图