Enable Dark Mode!
how-to-use-of-name-get-function-in-odoo.jpg
By: Hajaj Roshan

How to use name_get function in odoo

Technical

In Odoo ERP whenever we make a quotation, sale order, or in any case when we type a product name, we can see the suggestion of products shown in a dropdown list. In this list product names are preceded with a unique code called an internal reference. This is shown in the suggestion list for selecting exact products from two or more products that have the same name.

In this blog, the name_get() function of the Odoo platform is explained in detail.

This process of obtaining internal reference with the product name in the product list is done using the name_get() function in Odoo.

This is the Record(set) info of name_get(),

Model.name_get() ? [(id, name), ...]

It returns a literary representation for the records inside. 

name_get() function returns a list of pairs (id, text_repr) for each records and the return type is a list(tuple)

Now let us see how to define a name_get() function inside a model,

Here, I have a  custom model ‘profit.center’ which has 2 fields as shown in the below image,

how-to-use-of-name-get-function-in-odoo

Now whenever I call this model using a Many2one relation between other models, only the profit center name will be listed as shown below,

how-to-use-of-name-get-function-in-odoo

So, using the name_get() function in Odoo, let us see how we can add the short name of each profit center along with the profit center name.

For that we shall define the name_get() function inside the model ‘profit.center’.

Defining name_get ()

class ProfitCenterInherited(models.Model):
     _inherit = 'profit.center'
    def name_get(self):result = []
    for rec in self:result.append((rec.id, '%s - %s' % (rec.short_name,rec.name)))
    return result

Here “short_name” and “name” are the technical names of profit center short name and profit center name respectively.

Now if we run this code, we can see the desired output as shown below,

how-to-use-of-name-get-function-in-odoo

In this manner we can define the name_get() function inside any model for better clarification of data and helps to select the exact item from a list.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message