从其他模型中更改字段值
问题描述:
在pos.quotation模型中,我们有状态。所以我的目标是当状态发生变化时,我希望shoes.order中的布尔字段名为“handed”,并将变为true。我知道如何做到这一点,如果我会在一个模型中做到这一点,但当我需要改变其他领域的领域时,我会奋斗。从其他模型中更改字段值
class pos_quotation(models.Model):
_inherit = "pos.quotation"
@api.onchange('state')
def handed(self):
shoes = self.env['shoes.order']
for rec in self:
if self.state == "delivery_success":
rec.shoes.handed = True
答
电平变化中的自包含虚拟对象当你改变值数据库层上 没有发生。 (相对于依赖计算域)
但是原始值在self._origin中传递。
@api.onchange('state')
def handed(self):
if self.state == "delivery_success":
# first try this if it work
self._origin.shoes.handed = True
# if not working then you need to fetch the recorod
# from the database first.
shoes = self.env['shoes.order'].search[('id', '=', self.shoes.id)]
shoes.handed = True
但在onchange事件这样做可能会导致一些问题,成像用户具有 改变了主意,点击取消(更改discared),但shoes.handed是 人准备COMMITED数据库。
我对你的支持是使用相关领域。
class pos_quotation(models.Model):
_inherit = "pos.quotation"
# i'm assuming that your m2o field is shoes
# don't make readonly because you need to save it's changed value
# when you hit save.
handed = fields.Boolean(related="shoes.handed")
@api.onchange('state')
def handed(self):
if self.state == "delivery_success":
self.handed = True
不要忘了这个字段添加到表单视图,并确保它是无形的 所以用户不更新值手动
<field name="handed" invisible="1"/>
希望你有这个想法。