Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
7696 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
jan. 24
3401
2
okt. 23
5595
3
sep. 23
2435
0
mei 23
2509
1
mei 23
1992