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

Overriding method in a model

By
Thanh Loyal
on 3/13/16, 11:28 PM 505 views

Hello,

I found a guy had solved the same problem here:

\http://stackoverflow.com/questions/32859723/odoo-how-to-override-original-function

So I imitated to override a method of 'stock.inventory' class:


class StockInventoryInherit(models.Model):
_inherit = 'stock.inventory'

def _default_stock_location(self, cr, uid, context=None):
logger.info("_____________-------------------_____________")
logger.info("_____________-------------------_____________")
logger.info("_____________-------------------_____________")
return 19

But nothing changes.

What am I doing wrong?

Please help.

Thank you.

---


It seems to be that private methods (which start with underscore _) can not be override. I tested with a public method and it worked. But I want to override that private one. Any idea? thank you.

Thanh Loyal
on 3/14/16, 4:05 AM
0

Jignesh Mehta

--Jignesh Mehta--
2081
| 6 4 8
Surat, India
--Jignesh Mehta--


Jignesh Mehta
On 3/14/16, 12:47 AM

Hello Loyal,


For overriding method, You should called super() for it.


For Ex:-

class StockInventoryInherit(models.Model):

_inherit = 'stock.inventory'


    def _stock_inventory_default_stock_location(self, cr, uid, context=None):

        res = super(StockInventoryInherit, self)._stock_inventory_default_stock_location(cr, uid, context=context)

        return res


Hope it works for you.

Thanks,


Hi Jignesh Mehta, thank you for your answer. I am sorry, I did not make my self clear. My problem is my method was NOT called. Only the original method in 'stock' module was called.

Thanh Loyal
on 3/14/16, 12:51 AM

Hello, For override any base method, You should called super(). Just call print "res :::" in my method.

Jignesh Mehta
on 3/14/16, 1:33 AM

yeah I would call super() if my method was called. But currently Odoo does not run into my function. MY FUNCTION WAS NOT CALLED. My problem is only that.

Thanh Loyal
on 3/14/16, 3:41 AM

can you post your code ?

Jignesh Mehta
on 3/14/16, 3:52 AM

It seems to be that private methods (which start with underscore _) can not be override. I tested with a public method and it worked. But I want to override that private one. Any idea? thank you.

Thanh Loyal
on 3/14/16, 4:05 AM

My (main) code is like above, the method I want to override is stock.py --> class stock_inventory --> method _default_stock_location

Thanh Loyal
on 3/14/16, 4:10 AM

Hello, Try with my updated answer.

Jignesh Mehta
on 3/14/16, 4:13 AM

Hi Jignesh Mehta, thank you for your help. That still does not work. I have found the same question: https://www.odoo.com/forum/help-1/question/how-to-override-private-method-account-invoice-line-25187. . . If I override method 'action_done', it works just fine. But _default_stock_location does not. It seems to be maggical (something I still dont understand yet) keke.

Thanh Loyal
on 3/15/16, 12:11 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: 3/13/16, 11:28 PM
Seen: 505 times
Last updated: 3/14/16, 12:47 AM