In advance, excuse my poor english.
I need inherit some fields values in address from main contact. For that, i've created this code:
from odoo import api, models
class Partner(models.Model):
_inherit = "res.partner"
@api.model
def _l10n_cl_inherit_fields(self):
return ['l10n_cl_sii_taxpayer_type','l10n_cl_dte_email','l10n_cl_activity_description','l10n_cl_delivery_guide_price']
def read(self, fields=None, load='_classic_read'):
if any(elem in fields for elem in self._l10n_cl_inherit_fields()):
records = super().read(fields, load)
inherit_fields = [x for x in self._l10n_cl_inherit_fields() if x in fields]
for rcd in records:
no_value_fields = [x for x in inherit_fields if not rcd[x]]
if no_value_fields and rcd['parent_id']:
parent = self.env['res.partner'].browse(rcd['parent_id'][0])
for field in no_value_fields:
rcd[field] = rcd[field] or parent[field]
return records
return super().read(fields, load)
I'm new in python. Is there a better way for this?
What are you trying to do here? I mean why do you want to inherit existing fields? I understand you want to have a main contact, which has those fields with a value, and you want the child_ids record to have the same value.
Which is your edition?
If this is a recent version, this is not needed, since if the value is empty (False) then it gets the commercial_partner_id.l10n_cl_dte_email, for example.