Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Override an inherited function

By
SonnyV
on 2/2/15, 4:12 AM 881 views

Hello guys,

I'm trying to create an extension to the membership module. (I have called it x_membership)

Now I need to inherit a write and create function from the membership module.

The problem is that this function inherits account_invoice_line. So after a lot of trying and experimenting, I inherited the account_invoice_line in my x_membership module. This works, but now whenever I create a subscription, the method gets called twice, once from my module x_membership and once from the default membership module.

So my question is: how can I override the membership modules' create function? This function is in the membership.py under class account_invoice_line(osv.osv): _inherit='account.invoice.line' def write(......): res=super(account_invoice_line, self).write(.....) .......

I don't know how to tell odoo to override that method, because at this point I copied the entire account_invoice class from membership. But this calls super account invoice. I need it to be membership_account_invoice, but I don't know what name/syntax to use for this to happen.

The following is a simple image to try and clarify my question.
http://i.imgur.com/xFUvPSJ.png 

Thanks in advance, guys.

Sonny

0

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 2/2/15, 4:55 AM

If you don't want to call the parent's write method (which is VERY VERY dangerous, you need to basically replicate the whole function down to the method called in BaseModel), don't call the super().  But, why don't you want to call the parent's write method?

The way OpenERP inheritance works is that it will call x_membership's (assuming that it is the "last" module in the dependency tree) account.invoice.line write() method.  If it calls super(), assuming that your x_membership module depends on the membership module, it will can membership's account.invoice.line write() method.  When (I believe it will) it calls super(), it will call the parent's account.invoice.line's write method() so on and so forth until it reach orm.BaseModel's write() method.

0
SonnyV
On 2/2/15, 5:06 AM

@Ivan

I do want to call the parent's write method, but I don't want the membership module to also call the account_invoice_line's write method. Because now whenever I create a new membership, the write method gets called in membership and in x_membership, resulting in two added lines, while I only expect one line.

I'm not entirely sure if my x_membership module depends on membership. In __openerp__.py I have depends : ['membership'] though.

I'm fairly new too odoo and am not really experienced with the inheritance and dependancy-tree.

EDIT: I don't have enough karma, so I cannot comment to your comment :(

EDIT2: Is it possible to remove a line from the database? Since the membership module calls a create on membership.membership_line. And I only get one invoice, but two membership_lines. SO if I'm able to say remove the member_line created from the membership module before creating a new using x_membership, my error will be gone. Another workaround I was considering is to create a new table x_membership.membership_line and only show this one instead the membership.membership_lines data. But I'd like to have this as my last resort

As I mentioned earlier if your module depends on membership, it WILL call the snippet of account.invoice.line's write method() in BOTH x_membership and membership if you have super() call. Having said that, it should not have created two lines. With this, I think the code need fixing. Basically when you code the write() method snippet in x_membership, it needs to account for the snippet in membership module. You can call super() first, undo whatever changes performed by the snippet in membership module, before running whatever in x_membership. Or if you can, try to prevent whatever code in the snippet in membership module. But you have to go through that snippet so that eventually BaseModel's snippet is called. There are ways to "replace" the snippet in membership module BUT it will break membership module.

Ivan
on 2/2/15, 5:35 AM

Yes, when I comment the create method in the membership module I only get one line added. But I shouldn't be making adjustments in official modules. That's why I wanted to extend this module. In java it's easy to extend classes. Why isn't this as easy in python? :D Anyway, overwriting a method is fairly simple, but not if the method you want to override is already an inheritance from something. Image of the method I try to inherit. Notice it is located in membership.py and inherits account.invoice.line. What I need now is to overwrite THIS entry, not the account.invoice.line method. Because, if I'm right about this, when I'm able to inherit the membership's method, it will automatically inherit the account invoice line. http://i.imgur.com/lffFsKY.png The next image shows what I did in my custom module. http://i.imgur.com/HIlZA0s.png

Cats&Dogs bvba, SonnyV
on 2/2/15, 8:39 AM

Sorry, it is not clear WHICH entry. As I've mentioned you CANNOT, with the way OpenERP do inheritance, make the snippets in membership to be NOT executed. Also, which record is created twice? From what I see, the first image shows that membership module inherits and override write (which is collapsed), unlink, and write. The second image only shows that account.invoice.line is inherited again with nothing. So, by the look of these two images, there should not be double record creation.

Ivan
on 2/2/15, 9:24 AM

If you notice, the create method in the membership.py will also create the membership.membership_line if the membership is already in effect ( starts before today and ends after today ).

Ivan
on 2/2/15, 9:28 AM

The membership.membership_line is created twice. I copied the entire function to my x_membership module. The second image, where I inherit, has the same functions (write, unlink and create). I did this because I wanted x_membership to OVERRIDE membership's account_invoice_line's part. Whenever I comment the entire create in membership module (thus breaking it), I only get one record in membership_lines, which is what I need.

Cats&Dogs bvba, SonnyV
on 2/2/15, 9:39 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/2/15, 4:12 AM
Seen: 881 times
Last updated: 3/16/15, 8:10 AM