Skip to Content
Menú
This question has been flagged
1 Respondre
7644 Vistes

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
Best Answer

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
Related Posts Respostes Vistes Activitat
1
de gen. 24
3357
2
d’oct. 23
5565
3
de set. 23
2415
0
de maig 23
2471
1
de maig 23
1962