我正在制作一个向合作伙伴表单添加其他字段的模块。由于合作伙伴公司的价值相同,因此公司所有员工的价值也必须相同。
问题是我无法再编辑该字段...
我的代码:
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
答案 0 :(得分:1)
一个简单的解决方案是使用Odoo的商业领域逻辑:
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