Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
4315 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
2
říj 20
3370
2
čvc 24
2551
2
srp 25
2589
1
čvc 25
994
1
srp 25
1151