Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda

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
Buang
Jawaban Terbai

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
Buang
Post Terkait Replies Tampilan Aktivitas
1
Jan 24
3366
2
Okt 23
5567
3
Sep 23
2416
0
Mei 23
2477
1
Mei 23
1965