Skip to Content
Menu
This question has been flagged
Hi, I would like to know if there is a way to include this code in the Odoo user interface, and exactly where to put it because I would like to implement it.




def name_get(self):

  marcas_list = []

  for record in self:

      name = record.x_name + record.x_desc

      marcas_list.append((record.id, name))

  return marcas_list

Avatar
Discard
Best Answer

Hello,

To include the 'name_get' method in the Odoo user interface, you need to define it in the Python class of the model where you want it to be used. Typically, 'name_get' is used to customize the display of records in many2one fields or in various views where the record name is displayed.

Here's how you can implement it in your Odoo module:

1. Find or create the Python class corresponding to the model where you want to use 'name_get'. This class should inherit from 'models.Model'.

2. Add the 'name_get' method to the class. Ensure that the method takes 'self' as the first parameter and returns a list of tuples '(id, name)' where 'id' is the ID of the record and 'name' is the display name you want to show for that record.


Here's an example of how your class might look:

from odoo import models, fields


class YourClassName(models.Model):

    _name = 'your.model.name'  # Replace with the actual model name

    _description = 'Your Model Description'


    x_name = fields.Char(string='Name', required=True)

    x_desc = fields.Char(string='Description')


    def name_get(self):

        marcas_list = []

        for record in self:

            name = record.x_name + ' ' + record.x_desc

            marcas_list.append((record.id, name))

        return marcas_list

Replace 'your.model.name' with the actual model name where you want to use this method. Also, ensure that 'x_name' and 'x_desc' match the field names in your model.


After adding this method to your model class, it should be automatically used in the user interface wherever the record name is displayed.


Hope it helps.



Avatar
Discard
Related Posts Replies Views Activity
1
Dec 22
6112
0
Mar 15
3913
2
Mar 15
10264
2
Sep 24
6106
0
Mar 15
3961