Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
4322 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
paź 20
3379
2
lip 24
2557
2
sie 25
2604
1
lip 25
1013
1
sie 25
1151