Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1335 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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


Awatar
Odrzuć
Autor

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?

Powiązane posty Odpowiedzi Widoki Czynność
1
cze 25
5052
3
lip 20
11682
4
paź 24
5236
0
lis 16
4041
1
sie 15
4605