Naitik
Naitik

Reputation: 816

How to inherit or orverride @classmethod in odoo

I want to inherit @classmethod of class BaseModel(object)

How to inherit or override the @classmethod in our custom module ?

Upvotes: 0

Views: 1014

Answers (1)

Holden Rehg
Holden Rehg

Reputation: 927

I just ran into this today :)

You can extend it in a couple of ways. It depends if you really need to extend BaseModel or if you need to extend a specific sub class of BaseModel.

Sub Class

For any sub class you can inherit it as you would normally:

from odoo import api, fields, models

class User(models.Model):
    _inherit = 'res.users'

    @classmethod
    def check(cls, db, uid, passwd):
        return super(User, cls).check(db, uid, passwd)

Extend BaseModel Directly

In the case of BaseModel itself you are going to need to monkey patch:

from odoo import models

def my_build_model(cls, pool, cr):
    # Make any changes I would like...

    # This the way of calling super(...) for a monkey-patch
    return models.BaseModel._build_model(pool, cr)

models.BaseModel._build_model = my_build_model

Upvotes: 1

Related Questions