Skip to Content
Menu
This question has been flagged
3 Replies
8062 Views

I have a one2many relation where the many end model has a selection field and a method that computes a value from the selection and some other values and then returns the computed value and uses it for calculation with the values of dependent fields. (tried onchange, same result)

The problem is when I get values this way for my computed fields in my tree view with option editable="bottom" it computes the correct value when I press save, but when edit the record fields of any dependent field the value just becomes 0, until I save it again and only then the correct value is displayed.

I want it to show the the computed value when changing the dependent fields just like if I only use field values and not methods.

Is this possible? If yes an example would be nice, if not... at least I don't waste my time with it and have to try different approach.

And getting the value from fields and calculate it in the same model is a NO GO, the method uses some stuff that cannot be hard coded but it has to be dependant on user input.

Avatar
Discard
Author Best Answer

Did a work around. Now it works but I have to have 2 nearly identical functions that do the exactly same thing in two models.

The main problem was that I haven't found a way to pass the object state to other module. So every time the dependent fields triggered the the compute method that called the method in the other model that method fetched the data form the database record so that's why the the computed field didn't change or had a 0 value and it showed the correct value only after saving.

Now I call the record with the selection and stored expression from the 1st model, change the variables that are in the expression with the appropriate values and then I call the 2nd models method for calculating. Before the method for calculation changed the variables with values.

Avatar
Discard
Best Answer

You can use the @api.depends , it will do the onchange function too. whenever a change in the field specified in depends function, it will trigger the defined function and returns the result.

@api.multi
@api.depends('name', 'state')
def name_get(self):
result = []
for move in self:
if move.state == 'draft':
name = '* ' + str(move.id)
else:
name = move.name
result.append((move.id, name))
return result


Here is an example, here if ther is any change in the fields name, state will trigger the function. Here you can also use the fields names as 

@api.depends('debit', 'credit', 'move_id.matched_percentage', 'move_id.journal_id')

Here move_id can be a many2x or one2x field



Avatar
Discard
Author

Nope... this is not the answer. As I stated in the 1st paragraph "calculation with the values of dependent fields" that means that I used @api.depends. The "dependant" fields are the ones that are in the brackets "@api.depends( in here )". The method is in the other model. I know how to do such simple methods that you put into the example and as I stated the data is in different models that has to be collected by the method in the One2Many (on the many end side of relation) model. Thank you for trying to help but I see that you didn't read the whole question or didn't understand it. So please deselect your answer as the correct one!

Related Posts Replies Views Activity
2
Dec 23
2020
1
Jul 18
7577
3
Oct 23
4383
1
Mar 23
1253
2
Jan 23
3781