关于<xpath position="attributes"> 的问题
- 
本人遇到一个问题,代码如下: 
 <record id="sale_view_order_form" model="ir.ui.view">
 <field name="name">sale.order.form</field>
 <field name="model">sale.order</field>
 <field name="inherit_id" ref="sale.view_order_form"/>
 <field name="arch" type="xml">
 <field name="partner_id" position="after">
 <field name="boatmodel_id" />
 </field>
 <xpath expr="//field[@name='product_id']" position="attributes">
 <attribute name="context">{'boatmodel_id' : parent.boatmodel_id}</attribute>
 </xpath>
 </field>
 </record>
 问题:跟踪product.product 的search 发现{'boatmodel_id' : parent.boatmodel_id} 没有传给 context
 不知何故?
 context 为: dict: {u'lang': u'en_US', u'shop': 1, u'tz': u'Asia/Hong_Kong', u'uid': 1, u'pricelist': 1, u'partner_id': 5, u'uom': 1, u'quantity': 1}
 sale.view_order_form 其中代码为:
 <field name="product_id" context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}" groups="base.group_user" on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)"/>
- 
先给一段代码,慢慢体会下。 
 <record id="sale_view_order_form" model="ir.ui.view">
 <field name="name">sale.order.form</field>
 <field name="model">sale.order</field>
 <field name="inherit_id" ref="sale_stock.view_order_form_inherit"/>
 <field name="arch" type="xml">
 <data>
 <field name="partner_id" position="after">
 <field name="boatmodel_id" />
 </field>
 <xpath expr="//field[@name='product_id']" position="attributes">
 <attribute name="context">{'xxxx':'xxxx', 'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}</attribute>
 </xpath>
 </data>
 </field>
 </record>
- 
几点说明: 
 1、inherit_id 不是 sale.view_order_form,而是 sale_stock.view_order_form_inherit。这是因为sale.view_order_form的 product_id field 被 sale_stock.view_order_form_inherit replace 替换了。
 因此,[color=red]必须继承替换的VIEW ID[/color]。
 2、<data> 标签,表示多个更改,虽然有时不用 data 也可以用,但严重建议遵循官方规范 [检测到链接无效,已移除] br />
 3、<attribute name="context"> 此处值为替换,因此需要把原来的 context 值写上。 如 {'xxxx':'xxxx', 'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}
 另外,需要提醒的是:我在测试中发现 [color=red]xpath 只匹配第一个符合的node[/color] (存疑,待后续求证)
 <xpath expr="//field[@name='product_id']" position="attributes">
 因此这样只替换了 sale.order.line form view 的 product_id field ,如需替换 sale.order.line tree view 则需另外加一段,如:
 <xpath expr="//tree/field[@name='product_id']" position="replace">
 。。。。。。
 </xpath>
 谢谢 !
- 
感谢,感谢! 
 昨晚也搜索到了 sale_stock.view_order_form_inherit ,试了一下,未果就放弃了。
 大师一出手,说得明明白白,一片 雨过天清 豁然开朗 感觉,苦想两天的问题终于明白了。
 buke,Thanks!
 [attachimg=1]
 附上 sale_stock.view_order_form_inherit 部份代码:
 sale_stock_view.xml
 <record[color=red] id="view_order_form_inherit"[/color] model="ir.ui.view">
 [color=blue]<field name="name">sale.order.form.sale.stock</field>[/color]
 <field name="model">sale.order</field>
 <field name="inherit_id" ref="sale.view_order_form"/>
 <field name="arch" type="xml">
 <data>
 ......
 <field name="product_id" position="replace">
 <field name="product_id"
 context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}"
 groups="base.group_user"
 on_change="product_id_change(parent.pricelist_id,product_id,product_uom_qty,product_uom,product_uos_qty,product_uos,name,parent.partner_id, False, True, parent.date_order, product_packaging, parent.fiscal_position, False, context)"/>
 </field>
