Search code examples
xmlpython-2.7odoo-10

Odoo name_get how to shows different display name in two many2one field?


I work with Odoo(v10), I got two many2one field with same relation :

product_id (sale.order)

product_id = fields.Many2one(
    'product.product', 'Product',
    domain=[('type', 'in', ['product', 'consu'])], index=True, required=True,
    states={'done': [('readonly', True)]})

and product_id (stock.move)

product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product')

,then I have two field char : name (product.product) and kode_produksi (product.product)

 name = fields.Char(string='name') 
 kode_produksi = fields.Char(string='Kode Produksi')

My goal : product_id (sale.order) will display value base on field name and product_id (stock.move) will display value base on field kode_produksi, can someone help this problem?Thanks in advance


Solution

  • Ok , I solved by my self and I did it , try this code ..

    model:

     _inherit = "product.product"
    
    @api.multi
    def name_get(self):
        result = []
        for record in self:
            if self.env.context.get('product_id', False):
                # Only goes off when the custom_search is in the context values.
                name = str(record.name)
                result.append((record.id, "{}".format(name)))
            else:
                kode_produksi = str(record.kode_produksi)
                result.append((record.id, kode_produksi))
        return result
    

    on that code context default is False and will show data in field kode_produksi as display name many2one .If I put xml code like this :

                        <field name="product_id"  context="{'product_id':1}" />
    

    it will show data in field name as display name many2one