Search code examples
pythonodooodoo-10

how to know from which class a method of another class is called in odoo


I have 2 models

  • 1: example.orderline
  • 2: product.product

which are in separate classes

there is a many2one field "stock" in example.orderline stock=fields.Many2one("product.product","Product")

I want to override name_get method of model "product.product" for field "stock" which I did successfully:

def name_get(self):
     result = []
     for record in self:
        default_code = record.default_code
        result.append((record.id, default_code))

    return result

But the above also applies to Sale order and purchase order. how to name get method only applies for example.orderline model?


Solution

  • You can use context attribute in example.orderline model view side to handle mentioned situation.

    For example:

    <field name="stock" context="{'display_my_name': True}"/>
    

    Now check context value in name_get() method. If we find our context key, execute our custom logic, otherwise return super.

    For example:

    def name_get(self):
        if 'display_my_name' in self._context and self._context.get('display_my_name')
            result = []
            for record in self:
                default_code = record.default_code
                result.append((record.id, default_code))
            return result
        else:
            return super(YourClass, self).name_get()
    

    In this way, it will not disturb other form view field value display.