跳至内容
菜单
此问题已终结
1 回复
1324 查看

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

形象
丢弃
最佳答案

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 :)


形象
丢弃
编写者

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?

相关帖文 回复 查看 活动
1
6月 25
5042
3
7月 20
11656
4
10月 24
5228
0
11月 16
4025
1
8月 15
4598