odoo 开发基础 -- 视图之xpath语法
odoo 视图函数 在整个项目文件中,结构并不是十分明显,虽然它也遵循MVC设计,类比django的MTV模式,各个模块区分的十分明显,在Odoo中,视图的概念不是特别明显,很多时候,我们会将调用模型的函数直接写在models里边(即:类中)。
而对于模版Tempelate部分,odoo里边反而称做“views”,如下是odoo典型模块,销售模块sale的代码结构:

可以看到odoo的前端显示部分,对应的就是views,它是基于xml来实现的,而不是我们日常的html;
今天要说的xpath语法,就是针对于xml文件的一种语法格式。
通过xpath语法,可以对xml文件中要显示的内容,进行自定义的显示,比如:我们要插入一个新的标签在当前已经存在的标签的后边,前边,里边甚至替换。。。
这种语法看着还有点像JS的寻找父子节点。。。
1. xpath有一个用法,就是当你要修改的多个标签是在同一个目录节点下的时候,多个xpath标签可以写在同一个<record>标签中,而不用每次都去声明定义一遍,

支持的视图类型:form、tree、search ...
支持的定位方法:
<notebook position="inside">
<xpath expr="//page[@name='page_history']" position="inside">
<field name="mobile" position="after">
<filter name="consumable" position="after">
<group name="bank" position="after">
<xpath expr="//field[@name='standard_price']" position="replace">
<xpath expr="//button[@name='open_ui']" position="replace">
<xpath expr="//div[@class='oe_employee_details']/h4/a" position="after">
<xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/tree/field[@name='analytic_account_id']" position="replace">
<xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/form/group/field[@name='analytic_account_id']" position="replace">
支持的规则:before、after、replace、inside、attributes
插入:
position='before'
position="after"
<field name="mobile" position="after">
<field name="sale_order_count"/>
</field>
<filter name="consumable" position="after">
<separator/>
<filter name="filter_to_qty_available" string="在手数量>0" icon="terp-accessories-archiver+" domain="[('qty_available', '>', 0)]"/>
<filter name="filter_to_virtual_available" string="预测数量>0" icon="terp-accessories-archiver+" domain="[('virtual_available', '>', 0)]"/>
</filter>
替换:
position="replace"
<xpath expr="//field[@name='standard_price']" position="replace">
<group name='cost_prices' colspan="2" col="4">
<field name="standard_price" nolabel="1" attrs="{'readonly':[('cost_method','=','average')]}"/>
<field name="cost_price_extra" groups="product.group_product_variant"/>
</group>
</xpath>
<xpath expr="//button[@name='open_ui']" position="replace">
<button name="open_ui" type="object" string="Start Selling" attrs="{'invisible' : [('pos_state', 'not in', ('opened',))]}" class="oe_highlight" invisible="True"/>
</xpath>
内部创建:position="inside"
<xpath expr="//page[@name='page_history']" position="inside">
<group name="grp_task" string="Tasks">
<field name="task_ids" colspan="4" nolabel="1">
<tree string="Tasks" editable="bottom">
<field name="name"/>
<field name="user_id"/>
<field name="date_deadline"/>
<field name="state" invisible="1"/>
<button name="do_open" states="pending,draft,done,cancelled" string="Start Task" type="object" icon="gtk-media-play" help="For changing to open state" invisible="context.get('set_visible',False)"/>
<button name="action_close" states="draft,pending,open" string="Done" type="object" icon="terp-dialog-close" help="For changing to done state"/>
</tree>
</field>
</group>
</xpath>
修改属性:修改属性能够实现的功能,不要使用 replace
position="attributes"
<xpath expr="//field[@name='name']" position="attributes">
<attribute name="required">1</attribute>
</xpath>
示例代码:
<record id="product_normal_variant_form_view" model="ir.ui.view">
<field name="name">product.normal.variant.form</field>
<field name="model">product.product</field>
<field name="type">form</field>
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='name']" position="attributes">
<attribute name="required">1</attribute>
</xpath>
<xpath expr="//field[@name='standard_price']" position="replace">
<group name='cost_prices' colspan="2" col="4">
<field name="standard_price" nolabel="1" attrs="{'readonly':[('cost_method','=','average')]}"/>
<field name="cost_price_extra" groups="product.group_product_variant"/>
</group>
</xpath>
<sheet>
<group col="2" colspan="2" groups="base.group_extended" position="replace">
<group colspan="2" col="6" name="weight" groups="base.group_extended">
<field name="is_multi_variants" invisible="1"/>
<group colspan="2" col="2">
<separator string="Template Weights" colspan="2"/>
<field digits="(14, 3)" name="volume" attrs="{'readonly':[('type','=','service')]}"/>
<field digits="(14, 3)" name="weight" attrs="{'readonly':[('type','=','service')]}"/>
<field digits="(14, 3)" name="weight_net" attrs="{'readonly':[('type','=','service')]}"/>
</group>
<group colspan="2" col="2" attrs="{'invisible':[('is_multi_variants','=',False)]}">
<separator string="Variant Weights" colspan="2"/>
<field digits="(14, 3)" name="additional_volume" attrs="{'readonly':[('type','=','service')]}"/>
<field digits="(14, 3)" name="additional_weight" attrs="{'readonly':[('type','=','service')]}"/>
<field digits="(14, 3)" name="additional_weight_net" attrs="{'readonly':[('type','=','service')]}"/>
</group>
<group colspan="2" col="2" attrs="{'invisible':[('is_multi_variants','=',False)]}">
<separator string="Total Weights" colspan="2"/>
<field digits="(14, 3)" name="total_volume"/>
<field digits="(14, 3)" name="total_weight"/>
<field digits="(14, 3)" name="total_weight_net"/>
</group>
</group>
</group>
</sheet>
</data>
</field>
</record>
<!-- 隐藏 open_ui 按钮 -->
<record model="ir.ui.view" id="pos_session_opening_form_view_openui">
<field name="name">pos.session.opening.form.view.openui</field>
<field name="model">pos.session.opening</field>
<field name="inherit_id" ref="point_of_sale.pos_session_opening_form_view"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='open_ui']" position="replace">
<button name="open_ui" type="object" string="Start Selling" attrs="{'invisible' : [('pos_state', 'not in', ('opened',))]}" class="oe_highlight" invisible="True"/>
</xpath>
</field>
</record>
<record id="product_search_form_view_filter" model="ir.ui.view">
<field name="name">product.search.form.filter</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_search_form_view"/>
<field name="arch" type="xml">
<filter name="consumable" position="after">
<separator/>
<filter name="filter_to_qty_available" string="在手数量>0" icon="terp-accessories-archiver+" domain="[('qty_available', '>', 0)]"/>
<filter name="filter_to_virtual_available" string="预测数量>0" icon="terp-accessories-archiver+" domain="[('virtual_available', '>', 0)]"/>
</filter>
</field>
</record>
<!-- Partner kanban view inherte -->
<record model="ir.ui.view" id="crm_lead_partner_kanban_view">
<field name="name">res.partner.kanban.saleorder.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.res_partner_kanban_view"/>
<field name="priority" eval="20"/>
<field name="arch" type="xml">
<field name="mobile" position="after">
<field name="sale_order_count"/>
</field>
<xpath expr="//div[@class='oe_kanban_partner_links']" position="inside">
<a name="%(sale.act_res_partner_2_sale_order)d" type="action" t-if="record.sale_order_count.value>0">
<t t-esc="record.sale_order_count.value"/> Sales
</a>
</xpath>
</field>
</record>
<!-- Partners inherited form -->
<record id="view_task_partner_info_form" model="ir.ui.view">
<field name="name">res.partner.task.info.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='page_history']" position="attributes">
<attribute name="invisible">False</attribute>
</xpath>
<xpath expr="//page[@name='page_history']" position="inside">
<group name="grp_task" string="Tasks">
<field name="task_ids" colspan="4" nolabel="1">
<tree string="Tasks" editable="bottom">
<field name="name"/>
<field name="user_id"/>
<field name="date_deadline"/>
<field name="state" invisible="1"/>
<button name="do_open" states="pending,draft,done,cancelled" string="Start Task" type="object" icon="gtk-media-play" help="For changing to open state" invisible="context.get('set_visible',False)"/>
<button name="action_close" states="draft,pending,open" string="Done" type="object" icon="terp-dialog-close" help="For changing to done state"/>
</tree>
</field>
</group>
</xpath>
</field>
</record>
odoo 开发基础 -- 视图之xpath语法的更多相关文章
- odoo 开发基础 -- 视图之widget
Odoo 中的widget many2many_tags one2many_list selection progressbar selection statusbar handle monetary ...
- odoo开发基础--模型之基本字段类型
定义模型的时候,和python的其他框架类似,可以对比Django,同样是一个模型即:一个class对应生成数据库中的一张表, 只是odoo的继承机制比较复杂一点,在日常的开发中,定义模型的时候, 基 ...
- odoo 开发基础 -- postgresql重新启动、状态查看
场景描述: 当遇到数据库不能正常访问的时候,我们首先想到的是,查看相关的告警日志,一般先查看系统的日志,然后查看数据库的日志,Linux平台下,postgresql的日志文件存放目录在如下路径: te ...
- 【实习第十天】odoo开发基础整合
前言 发文时间是2019年7月19日.提一下学习odoo的感受,odoo目前在国内并不是很流行,且主流是在企业型软件,所以导致目前odoo在网上的文献很少,学习相对来说比其他框架吃力.以下为大家总结1 ...
- 【实习第一天】odoo开发基础(一)
管理权限 在项目中,有个security文件夹,其中的ir.model.access文件后面带4个参数.分别代表着读,写,创建,删除的操作 想要开启权限需要将其参数调成为1,反之为0.倘若不调整参数, ...
- 【实习第二天】odoo开发基础(二)
搜索视图 搜索试图包括过滤器(Filters),分组(Group By)以及收藏(Favorites) 其中还包括默认的搜索栏 搜索栏添加自定义方法 <!--views.xml--> &l ...
- odoo开发笔记 -- 视图继承扩展
参考: http://www.jeffzhang.cn/Odoo-Notes-2/ http://blog.csdn.net/zhangfeng1133/article/details/4693517 ...
- Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础
本节内容: 1. 面向对象高级语法部分 1.1 静态方法.类方法.属性方法 1.2 类的特殊方法 1.3 反射 2. 异常处理 3. Socket开发基础 1. ...
- iOS开发——总结篇&IOS开发基础知识
IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...
随机推荐
- Winform自定义表单(转)
出处:http://www.newlifex.com/showtopic-167.aspx 好吧,附件真的损坏了,原始代码我也没有了,再提取我也没精力了,不好意思,哪位之前下过可以重发一遍吗?不过即使 ...
- BitMap的简单实现
面试结束的这些日子好几次接触到BitMap这个东西.到底是啥呢,究其原因就是虽然它的使用条件较为苛刻,但是它对应的时间复杂度和空间复杂度真的是惊人的好. 首先是根据其思想先写了一个比较差的实现代码: ...
- HTTP请求模型和头信息参考
发送HTTP请求:一个请求由四个部分组成:请求行.请求头标.空行和请求数据 请求行 请求行由三个标记组成:请求方法.请求URI和HTTP版本,它们用空格分隔.例如:GET /index.html HT ...
- How to transfer developer profile to one mac to another mac
Export developer profile from old mac. In the Xcode Organizer, select your team in the Teams section ...
- 【python-crypto】导入crypto包失败的情况,怎么处理
[python-crypto]导入crypto包失败的情况,怎么处理 是因为你自己安装的python的版本太高,所以自己降版本吧,捣鼓了一下午 pip install crypto pip insta ...
- noip第34课作业
1. 信息加密 [问题描述] 在传递信息的过程中,为了加密,有时需要按一定规则将文本转换成密文发送出去.有一种加密规则是这样的:1. 对于字母字符,将其转换成其后的第3个字母.例如:A→D,a→ ...
- android 首字母迷糊查询 拼音查询 中英文混排查询
对于这个问题,还没有动手去做,暂且查了查资料,把思路记录下来: 1. 数据库保存拼音+汉字.在插入数据库的时候将这些信息保存下来,将来可以进行首字母模糊查询,拼音查询,中英文混排查询(参考手机通讯录数 ...
- 主题模型之概率潜在语义分析(Probabilistic Latent Semantic Analysis)
上一篇总结了潜在语义分析(Latent Semantic Analysis, LSA),LSA主要使用了线性代数中奇异值分解的方法,但是并没有严格的概率推导,由于文本文档的维度往往很高,如果在主题聚类 ...
- 《A computer-aided healthcare system for cataract classification and grading based on fundus image analysis》学习笔记
Abstract This paper presents a fundus image analysis based computer aided system for automatic class ...
- Python自动化开发 - 模块与包
本节内容 一.模块 1.import module 2.from module import 3.from module import * 4.模块的__name__属性 5.模块搜索路径 6. ...