Ir al contenido
Menú
Se marcó esta pregunta

Hi,

How could we import csv data into a many2one field with a related field linked to it in order to avoid having multiple matches for a given value?

An example will give a clearer view. I would like to import options in the product.variant.dimension.value object of the product_variant_multi module. I have several identical option names for different dimensions (88 option for length and width dimension for example). The two fields of the product.variant.dimension.value object are described below:

class product_variant_dimension_value(osv.Model):
    _name = "product.variant.dimension.value"

    def _get_values_from_options(self, cr, uid, ids, context=None):
         return self.pool.get('product.variant.dimension.value').search(cr, uid, [('option_id', 'in', ids)], context=context)

    _columns = {
    'option_id' : fields.many2one('product.variant.dimension.option', 'Option', required=True, select = 2),
    'dimension_id' : fields.related('option_id', 'dimension_id', type="many2one", relation="product.variant.dimension.type", string="Dimension Type", store={
        'product.variant.dimension.value': (lambda self, cr, uid, ids, c={}: ids, ['option_id'], 10),
        'product.variant.dimension.option': (_get_values_from_options, ['dimension_id'], 20),
            }),}

class product_variant_dimension_option(osv.Model):
    _name = "product.variant.dimension.option"
    _columns = {
        'name': fields.char('Dimension Option Name', size=64, required=True),
        'code': fields.char('Code', size=64),
        'sequence': fields.integer('Sequence'),
        'dimension_id': fields.many2one('product.variant.dimension.type', 'Dimension Type', ondelete='cascade'),
    }

How could we manage to import same name but different options within a certain domain defined by the dimension name?

Example of the csv rows I would like to import into product.template object:

name        value_ids/dimension     value_ids/option
Frame       Width                               88
            Length                             88

Any suggestions are welcome...

Avatar
Descartar
Autor Mejor respuesta

Tried first to modify the base_import module but could not find how to do it.

So I have done it by creating two char fields in the class:

class product_variant_dimension_value(osv.osv):
_inherit = "product.variant.dimension.value"
_columns = {
            'dimension_name' : fields.char('Dimension Name', size=64, select=True),
            'option_name' : fields.char('Option Name', size=64, select=True),

and override the create method:

 def create(self,cr,uid,vals,context=None):
    if context is None:
        context = {}
    option_obj = self.pool.get('product.variant.dimension.option')
    if 'dimension_name' in vals.keys() and 'option_name' in vals.keys():
        option_id = option_obj.search(cr,uid,[('name','=',vals['option_name']),('dimension_id.name','=',vals['dimension_name'])])
        if len(option_id)==1:
            option_id = option_id[0]
        else:
            raise osv.except_osv(_('Matching error!', 
                                   _("Several or no options exists for %s / %s : %s" 
                                     % (vals['dimension_name'],vals['option_name'],option_id))))
        vals.update({'option_id':option_id})
    return super(product_variant_dimension_value,self).create(cr,uid,vals,context)

So from the csv file I will import the names only by entering dimension_name and option_name in the header:

name        value_ids/dimension_name     value_ids/option_name
Frame       Width                             88
            Length                             88

This allow us to deal with names only when creating the files to import instead of external ID numbering which starts to be heavy when having a lot of rows and columns to deal with.

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
may 24
2576
1
nov 23
1945
1
ene 19
3920
6
jun 18
5670
1
oct 16
11397