Class Inheritance in OpenERP

Asked by Cuong

Hi,

I have confused on class inheritance. Take an example. I have 3 classes: A, A1, A2 (these use class inheritance)

- In class A: I override osv write() method to do some general jobs.
- In class A1: A1 inherited from class A. I also override write() method
- In class A2: A2 inherited from class A. Method write() also is overridden.

Now, in class B, there are some codes like this
===
a_obj = self.pool.get('a')
a = a_obj.write(cr, uid, a_id, context=context)
===
 When write method is called, what happens? It will call A1's write() method, A2's write() method, A's write() method?

Please help me make clear this. In practice, I use a lot of inheritance, so I need to know the order of execution of the above write() call.

Thank you very much.

Question information

Language:
English Edit question
Status:
Answered
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Numérigraphe (numerigraphe) said :
#1

It will call the one that was loaded the last when the server initialized.
So you have to take care that :
- each call's it's superclass's write() method
- if each class is in a different module, the dependencies make sure A is loaded before A1 and A2.
Lionel Sausin.

Revision history for this message
Cuong (bhcuong2008) said :
#2

Hi,

In my case, each class exists in different module. A exists in the base module. A1, A2,.. exist in-dependency. It means A1 will require A. A2 will require A.

With your suggestion, there are 2 cases:
1. Just install A1, then when calling a_obj.write() will result in calling a1.write()

2. Install both A1, A2. How do I know the order of server initialization. A1 before A2 or A2 before A1? If A1 before A2, it will call a2.write(). Otherwise, A2 before A1, a1.write() will be called ?

In this case, a1.write() or a2.write() will be never called! How do I want to call specific a1.write() / a2.write() with self.pool.get('a').write()?

Thanks,

Revision history for this message
Numérigraphe (numerigraphe) said :
#3

Please understand that the openerp model's inheritance structure is distinct from python's class inheritance structure.
So, as long as you call super()'s method, the framework takes care of the rest. Trust it.

If you install A1 only: write() will call a1.write(), which will calls a.write()
if you install A1 before A2: write() will call a2.write(), which will call a1.write(), which will call a.write()
if you install A2 before A1: write() will call a1.write(), which will call a2.write(), which will call a.write()

If you do need a specific execution order, you must inform the framework with "depends".
Lionel.

Revision history for this message
Cuong (bhcuong2008) said :
#4

Thank you for your clear explanation.

You mention "depends", do you mean "depends" defined in file __openerp__?

Thank you very much,

Revision history for this message
Numérigraphe (numerigraphe) said :
#5

Yes, "depends" from __openerp__.py .
Lionel.

Can you help with this problem?

Provide an answer of your own, or ask Cuong for more information if necessary.

To post a message you must log in.