openerp override onchange behavior without affecting base

喜欢而已 提交于 2019-12-06 14:07:14

you need to overide the onchange function(you can use super() ) for the field 'product_id' and update the result. for example

def onchange_product(self,cr,uid,ids,product_id,context=None):
    values = super(<your_class_name>,self).onchange_product(cr, uid,ids,product_id,context=context)
    # values will be a dictionary containing 'value' as a key.
    # You need to add all the newly added related fields and other fields to the values['value']. 
    # if 'aaa' is the newly added field, then values['value'].update({'aaa':<value for aaa>})
    # then return values
    return values

modify you onchange to the following

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, context=None):
    values = super(purchase_order_line_custom, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned,name, price_unit, context=context)
    if product_id:
        product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
        values['value'].update({
            'product_type' : product.product_type,
            'product_subtype' : product.product_subtype,
            'qualified_name' : product.qualified_name,
            'count_id' : product.count_id                 
        })
    return values 

First let's understand why fields.related is used?

fields.related field that points to some data inside another field of the current record.

That means, fields which are related will be filled automatically when you will select the value of other field.

Example:

   _columns = {
       'product_id': fields.many2one('product.product', 'Product'),
       'product_name': fields.related('product_id', 'name', type='char', string='Product Name'),
    }

In this example product_name will be filled automatically when you will select the product_id. You don't have to write onchange for that.

You can find many examples for fields.related.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!