Skip to Content
Menu
This question has been flagged
1 Reply
3112 Views

Hi Everyone,


I'm trying to inherit odoo base function write_header which is defined inside BaseModel.


I've tried serveral examples but didn't helped, I also tried the following example which was succeded in earlier version of odoo.

from odoo.models import BaseModel

My TestClass(BaseModel)
def call_my_function(self):

print ("Triggered")

1/0

BaseModel.write_header = call_my_function

Any help is appriciated.


BR,

Vishnu

Avatar
Discard
Best Answer

Hi Vishnu,

A few comments about your code


1) your class declaration is missing the class keyword and the class name has a space (which is forbidden), try something like

class MyTestClass

2) When using the Odoo ORM, inheritance also requires a line after the class declaration such as

_inherit = 'base'

3) While the last line will succeed in changing the behavior of BaseModel.write_header, this is not proper inheritance but rather monkey-patching. The difference is you're changing the behavior of the BaseClass instead of creating a new sub-class with a different behavior. For proper inheritance, re-define the method like

def write_header(self): # Note: keep same method signature
    your_code_here_ideally_not_dividing_by_zero


Avatar
Discard
Related Posts Replies Views Activity
2
Oct 20
2334
2
Jul 24
1207
2
Nov 24
267
1
Oct 24
335
4
Oct 24
324