I want to take the values that have been filled in in res.company and display them in res.partner (Customes - Sales). so I added a new field. and want to display the company_registry according to the one in res.company. but I'm always wrong. Please help me...
from odoo import models, fields, api
class ResPartner(models.Model): _inherit = 'res.partner'
company_registry = fields.Char(string='Company Registry', compute='_compute_company_registry', store=True)
@api.depends('company_id') def _compute_company_registry(self): for partner in self: # Get the company_registry from the related company partner.company_registry = partner.company_id.company_registry if partner.company_id else False
@api.onchange('company_id') def _onchange_company_id(self): for partner in self: if partner.is_company and partner.company_id: partner.company_registry = partner.company_id.company_registry else: partner.company_registry = False
@api.model def create(self, vals): # If a company is set, get its company_registry if vals.get('is_company') and vals.get('company_id'): company = self.env['res.company'].browse(vals['company_id']) vals['company_registry'] = company.company_registry if company else False
return super(ResPartner, self).create(vals)
def write(self, vals): # If the company_id is changed, update the company_registry if 'company_id' in vals: company = self.env['res.company'].browse(vals['company_id']) if company: vals['company_registry'] = company.company_registry else: vals['company_registry'] = False
return super(ResPartner, self).write(vals)