Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
5571 Vistas

Friends,

In usual odoo selects the name field in many2one.

But how can i get other fields in many2one..(without using related)..

Eg:-I want to select the products in a new view of my custom module..

Thanks

 

Avatar
Descartar

please provide example or describe more.

Mejor respuesta

Hello,

If you wanted to extend the search on the many2one field then you need to override the name_search method like 

def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
    return super(class_name, self).name_search(cr, uid, name, args=args, operator=operator, context=context, limit=limit)

It will return List of tuple, which is returned by name_get method.

In odoo name_get method is responsible to get name on the many2one field like [p123] Product 123

def name_get(self, cr, uid, ids, context=None):
    rerturn return super(class_name, self).name_get(cr, uid, ids, context=context)

Hope this helps in understanding name_search & name_get method in odoo.

 

Avatar
Descartar
Mejor respuesta

You can change name_get and concatenate other fields. You can see example in addons.

For example if want to display field1 concatenated to field2 as "field1 / field2" in your many2one list use this code:

    def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['field1', 'field2'], context)
        res = []
        for record in reads:
            field1 = record['field1']
            field2 = record['field2']
            if field2:
                name = field2 + ' / ' + field1
            else:
                name =  field1
            res.append((record['id'], name))
        return res

    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
        if not args:
            args = []
        if not context:
            context = {}
        if name:
            # Be sure name_search is symetric to name_get
            ids=[]
            name_splited = name.split(' / ')
            if len(name_splited) > 1:
                field1 = name_splited[1]
                field2 = name_splited[0]
                ids += self.search(cr, uid, [('field1', operator, field1),('field2', operator, field2)] + args, limit=limit, context=context)
            else:
                ids += self.search(cr, uid, ['|',('field1', operator, name),('field2', operator, name)] + args, limit=limit, context=context)
        else:
            ids = self.search(cr, uid, args, limit=limit, context=context)
        return self.name_get(cr, uid, ids, context)

Avatar
Descartar