Odoo:计算字段,在某些情况下不更新?
Odoo : Computed fields, don't update in some case?
我有一个计算字段,它必须在字段更新时更新。
所以我使用 @api.depends('field1')
和 link 函数
field_to_compute: fields.integer(compute='the_function' store=True)
它工作正常。
但是知道我想在 field1
取值 A
时更新它,并在 field1
取值 B
时保持不变。但是 field_to_compute
的旧值是从数据库导入的,没有计算出来。
所以我有两个问题:
- 如何让用户自己设置值。 (我可以修改它,
但是当我创建一个新成员时,它将被计算并且只有在
第一个存档我可以修改UI)
我怎样才能做出像这样的东西:
@api.depends('field1')
def the_function(self):
value = self.field1
if value == A:
field_to_compute = 123123
elif value == B:
field_to_compute = stored_field_to_compute #field_to_compute
与之前存储的值保持相同
编辑(示例):
我在当前模型中的res.partner,继承了res.partner。
我有一个字段:
'model_state': field.char(compute='compute_type', string = 'Demand', store=True, readonly=True)
第二个 res.partner 继承了 res.partner,在另一个模块中,我有 2 个字段:grade
和 status
,分别是一个 int 和一个多对一。 compute_type 与 model_state 计算相同,方法相同。
我也在这个 res.partner 中,有一个 one2many 字段:link_ids
所以我的函数是:
@api.depends('link_ids.type', 'link_ids.result')
def compute_type(self):
for record in self:
if self.link_ids:
if self.link_ids.result == 'A':
if self.link_ids.type == 'type1':
record.model_state = 'Mytext'
record.grade = 15
record.state = 1 #this is an id
elif self.link_ids.result == 'B':
record.model_state = 'MySecondText'
record.state = 2 #this is an id
我不会把所有的东西都放上去,因为它就像 25 个 elif(不是所有的都差不多,如果等等等等)所以它不相关。我检查了很多时间是否没有任何修改 record.grade 如果没有对其进行任何操作,在其他方面或不知道,但它只是被清空了。
基本上,简化了,如果结果是"OK",我想做点什么来投票。并且把state的id改成"accepted",给他一个文字(比如标题),给他一个更好的成绩,如果不是,他有一个state的id"denied",给他一个其他文本,并保持与他实际相同的成绩。
如果值为 B,则什么也不做...或更好:仅在需要时设置字段值。类似于:
def _compute_foo(self):
for item in self:
if item.other_field != B:
item.foo = 'newvalue'
我认为你的问题是你没有像你应该为计算字段那样设置值(参见 item.foo = 'newvalue'
)。
试试:
@api.onchange('field1')
def function(self)
if value == A:
field_to_compute = 123123
elif value == B:
field_to_compute = stored_field_to_compute #field_to_compute keep the same value as the one stored before
您应该尝试关注,
@api.onchange('field1')
def function(self):
field_value = self.field_to_compute
if self.field1 == A:
field_value = 123123
self.field_to_compute = field_value
自己做一个prefetch(field_to_compute要存):
@api.multi
@api.depends('field_x')
def _compute_field_to_compute(self):
# prefetch
data = {d['id']: d['field_to_compute']
for d in self.read(['field_to_compute'])}
for record in self:
if record.field_x == '123456':
record.field_to_compute = '654321'
else:
record.field_to_compute = data.get(record.id, 'default')
对于需要在数据库 table 上保存其值的计算字段,我们使用“@api..depends('field_attributes')”装饰器,当有对定义的字段属性所做的更改,它将触发执行函数。
对于不需要在数据库中保存其值的计算字段table,我们不再需要这个“@api..depends('field_attributes') ", 重新加载表单将作为函数执行的触发器。但这样做的缺点是,我们将无法从该特定字段中搜索数据。
我有一个计算字段,它必须在字段更新时更新。
所以我使用 @api.depends('field1')
和 link 函数
field_to_compute: fields.integer(compute='the_function' store=True)
它工作正常。
但是知道我想在 field1
取值 A
时更新它,并在 field1
取值 B
时保持不变。但是 field_to_compute
的旧值是从数据库导入的,没有计算出来。
所以我有两个问题:
- 如何让用户自己设置值。 (我可以修改它, 但是当我创建一个新成员时,它将被计算并且只有在 第一个存档我可以修改UI)
我怎样才能做出像这样的东西:
@api.depends('field1') def the_function(self): value = self.field1 if value == A: field_to_compute = 123123 elif value == B: field_to_compute = stored_field_to_compute #field_to_compute
与之前存储的值保持相同
编辑(示例):
我在当前模型中的res.partner,继承了res.partner。 我有一个字段:
'model_state': field.char(compute='compute_type', string = 'Demand', store=True, readonly=True)
第二个 res.partner 继承了 res.partner,在另一个模块中,我有 2 个字段:grade
和 status
,分别是一个 int 和一个多对一。 compute_type 与 model_state 计算相同,方法相同。
我也在这个 res.partner 中,有一个 one2many 字段:link_ids
所以我的函数是:
@api.depends('link_ids.type', 'link_ids.result')
def compute_type(self):
for record in self:
if self.link_ids:
if self.link_ids.result == 'A':
if self.link_ids.type == 'type1':
record.model_state = 'Mytext'
record.grade = 15
record.state = 1 #this is an id
elif self.link_ids.result == 'B':
record.model_state = 'MySecondText'
record.state = 2 #this is an id
我不会把所有的东西都放上去,因为它就像 25 个 elif(不是所有的都差不多,如果等等等等)所以它不相关。我检查了很多时间是否没有任何修改 record.grade 如果没有对其进行任何操作,在其他方面或不知道,但它只是被清空了。
基本上,简化了,如果结果是"OK",我想做点什么来投票。并且把state的id改成"accepted",给他一个文字(比如标题),给他一个更好的成绩,如果不是,他有一个state的id"denied",给他一个其他文本,并保持与他实际相同的成绩。
如果值为 B,则什么也不做...或更好:仅在需要时设置字段值。类似于:
def _compute_foo(self):
for item in self:
if item.other_field != B:
item.foo = 'newvalue'
我认为你的问题是你没有像你应该为计算字段那样设置值(参见 item.foo = 'newvalue'
)。
试试:
@api.onchange('field1')
def function(self)
if value == A:
field_to_compute = 123123
elif value == B:
field_to_compute = stored_field_to_compute #field_to_compute keep the same value as the one stored before
您应该尝试关注,
@api.onchange('field1')
def function(self):
field_value = self.field_to_compute
if self.field1 == A:
field_value = 123123
self.field_to_compute = field_value
自己做一个prefetch(field_to_compute要存):
@api.multi
@api.depends('field_x')
def _compute_field_to_compute(self):
# prefetch
data = {d['id']: d['field_to_compute']
for d in self.read(['field_to_compute'])}
for record in self:
if record.field_x == '123456':
record.field_to_compute = '654321'
else:
record.field_to_compute = data.get(record.id, 'default')
对于需要在数据库 table 上保存其值的计算字段,我们使用“@api..depends('field_attributes')”装饰器,当有对定义的字段属性所做的更改,它将触发执行函数。
对于不需要在数据库中保存其值的计算字段table,我们不再需要这个“@api..depends('field_attributes') ", 重新加载表单将作为函数执行的触发器。但这样做的缺点是,我们将无法从该特定字段中搜索数据。