Skip to Content
Menu
This question has been flagged
1 Reply
1314 Views

What is the best coding pattern in Odoo to do something every time a certain field of a model is changed.


Example: MyModel has some fields foo, fii, bar, baz. When these fields changes I want to call some other methods to do some dependent logic..


I have solved this earlier with something like this



How would you solve it?


Thank you

Avatar
Discard
Best Answer

Hi, 

If you already _inherit other model into your custom module you can later invoke the method (function) within that inherited model.

for example if you do _inherit = ['sale.order'] you can invoke a method (function) inside that sale.order, for example an action_confirm() method which is defined inside the sale.order itself. 

Regarding the self.env['some.other.model'], this mostly used to record manipulation such as create/write/unlink records within it, and it can be directly used to call other method inside that model too. For example, doing self.env['sale.order'].search([('id', '=', 1)]).action_confirm() will allows you to search sale.order with ID of 1 and confirm it. This should work because you are inheriting sale.order into your module.

However, if you try to call action_post() that defined in other module, for example in account.move then it can be something like self.env['account.move'].search([('id', '=', 1)]).action_post(). This ORM will search an account move record with ID 1, then later validate it.

In the question you asked, you should add action like search or create or unlink into self.env['some.other.model'] before invoking foo_fii() into the ORM, depends on what foo_fii() method does to the records :)

I hope this helpful, if it does kindly please mark the answer as solved for future reference :)


Avatar
Discard
Author

This is not answering my question at all.
I'm asking about best CODING PATTERN, not how to call methods from other models.

Sorry if it doesn't seems to answer your question, perhaps what you mean is "the best practice" writing something like your solution, no?

Related Posts Replies Views Activity
1
Jun 25
5030
3
Jul 20
11637
4
Oct 24
5220
0
Nov 16
4009
1
Aug 15
4582