openerp override onchange behavior without affecti

2019-05-28 01:59发布

问题:

I have inherited from purchase.order.line. I have added a bunch of related fields. They are all related to fields in product.product via product_id in order_line.

What I want to achieve is that when a user selects or changes a product in the purchase order line form, the related fields should get refreshed/updated/populated with values of the selected product.

I have written the onchange method for this but I'm not sure how to invoke it from the inherited view? The product_id field was already there in the (parent) order_line view and it already has an onchange defined. How do I get the system to use my onchange as well?

I don't want to disturb the onchange_productid method that is already there in purchase.order.line. So either before my onchange or after it, it should continue with its standard functioning.

Thanks

EDIT: latest version (getting errors when any of the related many2one or selection fields has a value).

class purchase_order_line_custom(osv.osv):
    _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, 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({
                                'qualified_name':product.qualified_name,
                                'product_type' : product.product_type or None,
                                'product_subtype' : product.product_subtype,
                                'count_id':product.count_id or None 
        })
    return values   

_columns={
          'product_type': fields.related('product_id','product_type',type='selection', string='Product Type', selection=[('X','X'),('Y','Y'),('Z','Z')]),
          'product_subtype': fields.related('product_id','product_subtype',type='char', size=64, string='Sub-Type'),
          'qualified_name': fields.related('product_id','qualified_name',type='char', size=64, string='Qualified Name'),
          'count_id': fields.related('product_id','count_id',type='many2one',relation='product.mycount',string='Count')
          }

purchase_order_line_custom() 

回答1:

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 


回答2:

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.



标签: openerp