Odoo Help

0

How to inherit a def function ?

By
Maurice Agée
on 9/18/15, 11:12 AM 1,338 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?

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), }

Maurice Agée
on 9/21/15, 5:09 AM
2
Axel Mendoza
On 9/18/15, 3:10 PM

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

0

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})

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), }

Maurice Agée
on 9/21/15, 4:34 AM

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.

Serpent Consulting Services Pvt. Ltd.
on 9/21/15, 5:27 AM

See my updated code and try that code.

Serpent Consulting Services Pvt. Ltd.
on 9/21/15, 6:26 AM

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 Agée
on 9/21/15, 8:24 AM

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

Serpent Consulting Services Pvt. Ltd.
on 9/22/15, 3:54 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 9/18/15, 11:12 AM
Seen: 1338 times
Last updated: 10/20/15, 9:43 AM