This question has been flagged
8 Replies
11118 Views

I would like to edit 'onchange_product_id' to 'purchase' to add a new field from 'product_template' in 'purchase_order_line'.

Can I use a method like 'inherit' or should I write all the code again?

Avatar
Discard
Author

I test with this code , but no corect value in PO_line class purchase_order_line(osv.osv): #_table = 'purchase_order_line' _name = 'purchase.order.line' _inherit = 'purchase.order.line' def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=False, fiscal_position_id=False, date_planned=False, name=False, price_unit=False, state='draft', context=None): res = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned, name=name, price_unit=price_unit, state=state, context=context) if 'value' in res: res['value'].update({'unitestock_po': 'product.template.unitestock'}, {'uniteachat_po': 'product.template.uniteachat'}) return res _columns = { 'unitestock_po': fields.many2one('product.template.unitestock','Unité pour Stock',required=True), 'uniteachat_po': fields.many2one('product.template.uniteachat','Unité pour Achats',required=True), }

Best Answer

You could override the onchange_product_id in your model inherit to call super() to get the original values returned from onchange_product_id and add your new field value before return

Avatar
Discard
Best Answer

Hello Maurice,

First you have to inherit the view of PO to add your custom field in PO Line.

Ex:

<record id="purchase_order_form_inherit" model="ir.ui.view">
<field name="name">purchase.order.inherit.form</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="model">purchase.order</field>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/tree/field[@name='name']" position="after">
<field name="your_custom_field"/>
</xpath>
</field>
</record>

Now, override onchange_product_id() method, make a super call and add your value to custom field before return statement.

Ex:

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, state='draft', context=None):
res = super(your_class_name, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned,
name=name, price_unit=price_unit, state=state, context=context)
if 'value' in res:
res['value'].update({'your_custom_field': 'value_of_custom_field'})
return res

Hope the given snippet will help you.

EDIT: 

If your custom field is of relational field then you have to pass ID of the record.

Ex:

unitestock_po = self.pool.get('product.product').browse(cr, uid, product_id, context=context).unitestock_po_id
res['value'].update({'your_custom_field': unitestock_po and unitestock_po.id or False})
Avatar
Discard
Author

I test with this code, but no value in the PO-line ?? class purchase_order_line(osv.osv): #_table = 'purchase_order_line' _name = 'purchase.order.line' _inherit = 'purchase.order.line' def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=False, fiscal_position_id=False, date_planned=False, name=False, price_unit=False, state='draft', context=None): res = super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned, name=name, price_unit=price_unit, state=state, context=context) if 'value' in res: res['value'].update({'unitestock_po': 'product.template.unitestock'}, {'uniteachat_po': 'product.template.uniteachat'}) return res _columns = { 'unitestock_po': fields.many2one('product.template.unitestock','Unité pour Stock',required=True), 'uniteachat_po': fields.many2one('product.template.uniteachat','Unité pour Achats',required=True), }

Your both fields are of relational fields. Its not working because you are passing char value instead of record ID. You have to pass record ID of "product.template.unitestock" object.

See my updated code and try that code.

Author

I try with your last code , no result I try with your code but 'product.template' inplace of 'product.product' , no result To resum : I have a this class and It's good class product_template(osv.osv): _name = 'product.template' _inherit = 'product.template' _columns = { 'matiere': fields.many2one('matierenom','Choix de la nuance',required=True, ondelete='cascade'), 'certificat': fields.many2one('certificatnom','Choix du certificat',required=True, ondelete='cascade'), 'unitestock': fields.many2one('product.uom','Unité de Stockage',required=True, ondelete='cascade'), 'uniteachat': fields.many2one('product.uom','Unité pour achat',required=True, ondelete='cascade'), 'coefficient': fields.float('Coefficient Achats/Stock', required=True, digits=0,), } _sql_constraints = [ ('uniq_name', 'unique(matierenom,product_tmpl_id)',"--"), ('uniq_name', 'unique(certificatnom,product_tmpl_id)',"--"), ] _defaults = { 'coefficient': 1, } product_template() after I want update po_line with unitestock and uniteachat

Maurice, its hard to discuss unformatted code I guess, would love to redirect you to our online training at Udemy at just $99 (67%) discount https://www.udemy.com/odoo-technical/?couponCode=Mufaddal99