简体   繁体   中英

Fill many2many field from other many2many field

In product.template model, I have a one2many field(attribute_line_ids) making reference the following class:

class product_attribute_line(osv.osv):
_name = "product.attribute.line"
_rec_name = 'attribute_id'
_columns = {
    'product_tmpl_id': fields.many2one('product.template', 'Product Template', required=True, ondelete='cascade'),
    'attribute_id': fields.many2one('product.attribute', 'Attribute', required=True, ondelete='restrict'),
    'value_ids': fields.many2many('product.attribute.value', id1='line_id', id2='val_id', string='Product Attribute Value'),
}

On the other hand, in product.template created a many2one field making reference to a new model(comun.denominador):

'cm_id' : fields.many2one('comun.denominador','Comun denominador', select=True, ondelete='cascade')

So that when I select 'cm_id' it brings records to product.template from comun.denominador by means of on_change function.

This is my onchange_function:

def on_change_cm_id(self,cr, uid, ids,cm_id,context=None):
    context=context or {}
    attributes_product_template = []
    value = {}
    if ids:
        old_note_ids = self.pool.get('product.attribute.line').search(cr, uid,[('product_tmpl_id','in',ids)])
        self.pool.get('product.attribute.line').unlink(cr, uid, old_note_ids)
    attribute_cm_ids = []
    attribute_cm_ids = self.pool.get('attribute.comun.denominador.line').search(cr, uid, [('comun_denominador_id', '=', cm_id)])
    for attribute_id in self.pool.get('attribute.comun.denominador.line').read(cr, uid, attribute_cm_ids, ['attribute_comun_denominador_id', 'value_comun_denominador_ids']):
        attributes_product_template.append((0,0,{'value_ids':attribute_id['value_comun_denominador_ids'][0],'attribute_id':attribute_id['attribute_comun_denominador_id'][0]}))
    value.update(attribute_line_ids=attributes_product_template)
    return {'value':value}

These models store information in comun.denominador:

class product_attribute_line(osv.osv):
_name = "attribute.comun.denominador.line"
_rec_name = 'attribute_comun_denominador_id'
_columns = {
    'comun_denominador_id': fields.many2one('comun.denominador', 'Comun denominador', required=True, ondelete='cascade'),
    'attribute_comun_denominador_id': fields.many2one('product.attribute', 'Attribute', required=True, ondelete='restrict', readonly=True),
    'value_comun_denominador_ids': fields.many2many('product.attribute.value', id1='line_id', id2='val_id', string='Product Attribute Value'),
}

class comun_denominador(osv.osv):
_name='comun.denominador'
_rec_name='comun_denominador'
_columns = {
    'comun_denominador': fields.char('Común denominador', size=10),
    'code': fields.char('Código clasificación', size=10),
    'attribute_line_comun_denominador_ids' : fields.one2many('attribute.comun.denominador.line', 'comun_denominador_id', 'Atributos del comun denominador')             
    }

As you can see in my onchange_function all I want to do is to populate fields 'attribute_id' and 'value_ids' from 'product.attribute.line' model with the values 'attribute_comun_denominador_id' and 'value_comun_denominador_ids' belonging to 'attribute.comun.denominador.line' model.

Onchange function works great with fields many2one ('attribute_comun_denominador_id'->'attribute_id'), but in the case of many2many fields ('value_comun_denominador_ids'->'value_ids'), I get this error:

File "/home/odoo/openerp/fields.py", line 1568, in convert_to_cache
    raise ValueError("Wrong value for %s: %s" % (self, value))
    ValueError: Wrong value for product.attribute.line.value_ids: 1

Does anyone know how to do migration correctly between many2many fields?

def on_change_cm_id(self,cr, uid, ids,cm_id,context=None):
    context=context or {}
    attributes_product_template = []
    value = {}
    if ids:
        old_note_ids = self.pool.get('product.attribute.line').search(cr, uid,[('product_tmpl_id','in',ids)])
        self.pool.get('product.attribute.line').unlink(cr, uid, old_note_ids)
    attribute_cm_ids = []
    attribute_cm_ids = self.pool.get('attribute.comun.denominador.line').search(cr, uid, [('comun_denominador_id', '=', cm_id)])
    for attribute_id in self.pool.get('attribute.comun.denominador.line').read(cr, uid, attribute_cm_ids, ['attribute_comun_denominador_id', 'value_comun_denominador_ids']):
        attributes_product_template.append((0,0,{'value_ids':[(6,0,[attribute_id['value_comun_denominador_ids'][0]])],'attribute_id':attribute_id['attribute_comun_denominador_id'][0]}))
    value.update(attribute_line_ids=attributes_product_template)
    return {'value':value}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM