I have a computed field, that has to be updated when a field is updated.
So I use @api.depends('field1')
and link the function
field_to_compute: fields.integer(compute='the_function' store=True)
It works fine.
But know I'd like to update it when field1
take value A
and to remain the same when field1
take the value B
. But the old value of field_to_compute
was imported from a database and wasn't computed.
So I have two problems:
- How can I allow the user to set the value himself. (I can modify it, but when I create a new member, it will be computed and only after the first save I can modify it in UI)
How can I make something like:
@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
keep the same value as the one stored before
EDIT (example):
I'm in a res.partner in the current model, inheriting of res.partner. I've a field in it :
'model_state': field.char(compute='compute_type', string = 'Demand', store=True, readonly=True)
In a second res.partner inheriting of res.partner too, in an other module, I have 2 fields : grade
and status
, respectively an int, and a many2one. Computing the same compute_type as model_state, the same way.
I also in this res.partner, have a one2many field : link_ids
So my function is :
@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
I won't put everything because it's like 25 elif (not all about the same if etc etc) so it wouldn't be pertinent. I checked a lot of time if nothing was modifying record.grade if nothing is done on it, in else or dunno, but it's just being emptied.
Basically, simplified, I'd like to do something if the result is "OK" to a vote. And change the id of the state to "accepted", give a text (like a title), and give him a better grade, and if it's not, he have an id of the state "denied", give an other text, and keep the same grade as he has actually.
self.link_ids.result
the result from one2many like this ? where as link_ids may contains multiple records so there may be multiple results. And hope there is no mistake in code here is typo in depends actually it's@api.depends(...)
– Emipro Technologies Pvt. Ltd.self.link_ids.field_x == 'foobar'
comparisons will give you a singleton error with more than one entry in link_ids. – CZoellner