Odoo Help

Welcome!

This community 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.

0

How to inherit a def function ?

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

--Axel Mendoza--
10263
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 9/19/15, 12:18 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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