Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Xpath 增加 on_change 响应事件 分享及求指点



  • 为某些字段增加on_change 响应事件 ,可以使用xpath  修改属性:修改属性能够实现的功能,
    position="attributes"

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xpath expr=&quot;//field[@name=&#039;name&#039;]&quot; position=&quot;attributes&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute name=&quot;required&quot;&gt;1&lt;/attribute&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/xpath&gt;<br />
    



    本例为 discount 增加 响应事件,需求来自于实际折扣为具体金额,而系统中计算的全是百分比的discount,需要提供一个具体金额的输入点,折算为 百分比,同时,输入百分比 具体折算金额也应该显示。 则新增字段discount_value

    <br />&nbsp; &lt;xpath expr=&quot;//field[@name=&#039;discount&#039;]&quot; position=&quot;attributes&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute name=&quot;on_change&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  discount_change(product_uom_qty,price_unit,discount,context)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/attribute&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/xpath&gt;<br />
    


    py 对应代码  其中 'discount_value' 为新增加字段,

    <br /> def discount_change(self, cr, uid, ids, product_uom_qty=1,price_unit=0,discount=0,context=None):<br /> <br />&nbsp; &nbsp; &nbsp; &nbsp; value = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#039;discount_value&#039;:0,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; if product_uom_qty * price_unit &gt; 0:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  try:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value.update({<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;discount_value&#039;:&nbsp; ( product_uom_qty * price_unit)*discount*0.01<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except ZeroDivisionError:&nbsp; # 这个地方没删掉,,,copy 过来的 <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass<br />&nbsp; &nbsp; &nbsp; &nbsp; return {&#039;value&#039;: value}<br /><br />
    




    discount_value界面代码  其中<label  > 都没起效 

    <br />&lt;xpath expr=&quot;//div[@name=&#039;discount&#039;]&quot; position=&quot;after&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;label for=&quot;discount_value123&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;div name=&quot;discount_value&quot; groups=&quot;sale.group_discount_per_so_line&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;field name=&quot;discount_value&quot; string =&quot;discount_value1&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  on_change=&quot;discount_value_change(product_uom_qty,price_unit,discount_value,context)&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;label for=&quot;discount_value1235&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;/div&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;label for=&quot;discount_value1243&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;/xpath&gt;<br />
    





    [img [检测到链接无效,已移除] /img]


    加上

    <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xpath expr=&quot;//label[@for=&#039;discount&#039;]&quot; position=&quot;after&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;label for=&quot;discount_value123&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;/xpath&gt;
    




    排成。。。。。。
    [img [检测到链接无效,已移除] /img]

    用了一段超长的xpath 解决问题

    <br /><br /> &lt;xpath expr=&quot;//notebook/page[@string=&#039;Order Lines&#039;]/field[@name=&#039;order_line&#039;]/form[@string=&#039;Sales Order Lines&#039;]/group/group&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position=&quot;inside&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;field name=&quot;discount_value&quot; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  on_change=&quot;discount_value_change(product_uom_qty,price_unit,discount_value,context)&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;/xpath&gt;<br />
    




    顺便 发个  XPath 语法链接  http://www.w3school.com.cn/xpath/xpath_syntax.asp br />openerp学习笔记 视图继承(tree、form、search)http://www.cnblogs.com/cnshen/p/3164275.html
    界面问题解决了,实际测试时发现,discount 和discount_value 字段都设置了on_change 响应,而且都是改对方的值,杯具了,修改discount或discount_value  时 不停的执行。。。。on_change  死循环了
    [img [检测到链接无效,已移除] /img]

    求 指点[img2 [检测到链接无效,已移除] /img2]


  • 管理员

    需要再加个判断来实现不要重新返回数值。

    <br />&lt;xpath expr=&quot;//field[@name=&#039;discount&#039;]&quot; position=&quot;attributes&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;attribute name=&quot;on_change&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  discount_change(product_uom_qty,price_unit,discount,discount_value,context)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/attribute&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/xpath&gt;<br />
    



    <br />def discount_change(self, cr, uid, ids, product_uom_qty=1,price_unit=0,discount=0,context=None):<br />&nbsp; &nbsp; if discount_value == ( product_uom_qty * price_unit)*discount*0.01:<br />&nbsp; &nbsp; &nbsp; &nbsp; return {}<br />...<br />
    


    同理 discount_value_change 也要。



  • 3Q 后来这么干了一下,解决了。

    <br />if&nbsp; abs(newdiscount-discount)&gt;0.001:&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&#039;value&#039;: value}&nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return {&#039;value&#039;: {}}&nbsp; &nbsp; <br /><br />
    


  • 我通常在几个同时使用 on_change  的时候. 加一个 from field 的参数



  • 学习了!尝试一下!