I am making a module that is adding additional field to partner form. As the value is the same for the partner companies it would have to be the same for all employees of the company.
Succeded to write the code to copy to child_usrs:
The problem is I cannot edit the field anymore...
My code:
class SpgMovField(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
spg_mov = fields.Char(string='MOV', compute='_compute_mov_from_parent', help='Minimum Order Value')
@api.one
@api.depends('parent_id', 'parent_id.spg_mov')
def _compute_mov_from_parent(self):
if self.parent_id:
self.spg_mov = self.parent_id.spg_mov
elif type(self.id) is not models.NewId:
query = """
SELECT
spg_mov
FROM res_partner
WHERE id=%s"""
self.env.cr.execute(query, [self.id])
result = self.env.cr.dictfetchone()
self.spg_mov = result['spg_mov']
@api.one
def _inverse_persist_fields(self):
query = """
UPDATE res_partner
SET
spg_mov=%s
WHERE id=%s"""
params = [SpgMovField._optional(self.spg_mov)]
self.env.cr.execute(query, params)
self.env.invalidate_all()
@api.model
@api.onchange('parent_id')
def onchange_parent_id_new_mov(self):
old_res = super(SpgMovField, self).onchange_parent_id(self.parent_id.id)
_LOGGER.info(repr(old_res))
if type(old_res) is dict and 'value' in old_res:
for field, value in old_res.get('value').items():
if hasattr(self, field):
setattr(self, field, value)
if self.parent_id:
fields_to_copy = ['spg_mov']
for key in fields_to_copy:
self[key] = self.parent_id[key]
@staticmethod
def _optional(condition, value = None, defaultValue = None):
if value is None:
value = condition
return value if condition != False else defaultValue
A simple solution is to use Odoo's commercial fields logic:
class ResPartner(models.Model):
_inherit = "res.partner"
my_field1 = fields.Char()
my_field2 = fields.Char()
@api.model
def _commercial_fields(self):
""" Returns the list of fields that are managed by the commercial entity
to which a partner belongs. These fields are meant to be hidden on
partners that aren't `commercial entities` themselves, and will be
delegated to the parent `commercial entity`. The list is meant to be
extended by inheriting classes. """
commercial_fields = super(ResPartner, self)._commercial_fields()
new_commercial_fields = ['my_field1', 'my_field2']
commercial_fields.extend(new_commercial_fields)
return commercial_fields