Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
7646 Vistas

I tried to change the display name of the "res. partner" model by:

  • Specifying the "_rec_name" in a class that I created which inherits "res. partner" of course.
  • Override the "name_get" method as the following:
def name_get(self):
res = []
for partner in self:
name = partner.name
if partner.is_building:
name = partner.is_building
res.append((partner.id, name))
return res

It worked with the second solution but, when I click on the "save" button, the display name of the many2one field "partner_id" takes the value of the field "name" of "res. partner".


Avatar
Descartar
Mejor respuesta

Hi,

You should override the name field in your custom class if you wish to modify the partner_id field's display name when it appears in a form view or other areas of the user interface. To accomplish this, create a new field in your class that inherits the res.partner model, then set the name field to the value you want.

from odoo import models, fields

class CustomResPartner(models.Model):
_name = 'custom.res.partner'
_inherit = 'res.partner'

name = fields.Char(
    string='Name',
    compute='_compute_custom_name',
    store=True
)

is_building = fields.Char(string='Building Name')

@api.depends('is_building', 'name')
def _compute_custom_name(self):
    for partner in self:
        if partner.is_building:
            partner.name = partner.is_building
        else:
            partner.name = partner._origin.name


We replace the old name field with a new calculated field called name, and we inherit the res.partner model. The value of this new field is determined by using the is_building field. It uses that value as the name if is_building is set; if not, it uses the value from the original name.


Hope it helps


Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
ene 24
3362
2
oct 23
5566
3
sept 23
2415
0
may 23
2473
1
may 23
1963