Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
7664 Widoki

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".


Awatar
Odrzuć
Najlepsza odpowiedź

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


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
sty 24
3382
2
paź 23
5580
3
wrz 23
2419
0
maj 23
2488
1
maj 23
1975