我已经重写了product_id_change() of sale.order.line,它工作得很好。现在我的要求是在我的onchange中获得sale.order字段,那么如何从sale.order.line product_id_change()中获得sale.order的字段值?
这是我的代码odoov8
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0, uom=False, qty_uos=0, uos=False, name='', partner_id=False, lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
#how to access self.order_id
print "order id===", order_id
res = super(sale_order_line, self).product_id_change( cr, uid, ids, pricelist, product, qty=qty,uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,lang=lang, update_tax=update_tax, date_order= date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
return res发布于 2017-06-23 13:58:52
您可以使用以下步骤来完成它。
1.在On change方法中,可以传递一个参数父级。
<record model="ir.ui.view" id="sale_margin_sale_order_line">
<field name="name">sale.order.line.margin_and_quantities.view.form</field>
<field name="type">form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/form[@string='Sales Order Lines']/group/group[1]/field[@name='product_id']"
position="attributes"> position="attributes">
<attribute name="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, False, parent.date_order, False, parent.fiscal_position, True, context,parent)
</attribute>
</xpath>
</field>
</record>在上面的视图中,我们使用了位置属性选项来代替on_change方法。
在on_change方法中,只需在父级中添加一个参数,父级表示sale.order对象。
sale.order.line,中有product_id字段,您可以在视图中使用父关键字访问order_id。
2.在py文件中继承product_id_change方法。
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None,order_id=False):
res=super(sale_order,self).product_id_change(cr,uid,ids,pricelist,product,qty,uom,qty_uos,uos,name,partner_id,lang,update_tax,date_order,packaging,fiscal_position,flag,context)
return res在上面的方法中,order_id在方法参数中是可用的,因此可以直接访问它。 如果您已经安装了sale_stock模块,那么您应该继承py文件中的方法&在视图中更改product_id_change_with_wh on_change中的位置属性,还必须在openerp.py文件中提供sale_stock的依赖性。 之后,您将在product_id_change_with_wh on_change方法中获得order_id字段&在on_change product_id方法中传递此参数。
例:
def product_id_change_with_wh(self, cr, uid, ids, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, warehouse_id=False, context=None,order_id=False):
context = context or {}
product_uom_obj = self.pool.get('product.uom')
product_obj = self.pool.get('product.product')
warning = {}
#UoM False due to hack which makes sure uom changes price, ... in product_id_change
res = self.product_id_change(cr, uid, ids, pricelist, product, qty=qty,
uom=False, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context,order_id=order_id)发布于 2017-06-23 13:32:45
此方法是在旧API中编写的,因此您必须浏览更改后的sale.order.line记录。
def product_id_change(
self, cr, uid, ids, pricelist, product, qty=0, uom=False,
qty_uos=0, uos=False, name='', partner_id=False, lang=False,
update_tax=True, date_order=False, packaging=False,
fiscal_position=False, flag=False, context=None):
line = self.browse(cr, uid, ids[0], context)
# print line.order_id.name
res = super(sale_order_line, self).product_id_change(
cr, uid, ids, pricelist, product, qty=qty,uom=uom,
qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id
lang=lang, update_tax=update_tax, date_order= date_order,
packaging=packaging, fiscal_position=fiscal_position,
flag=flag, context=context)
return reshttps://stackoverflow.com/questions/44721532
复制相似问题