How to override method of BaseModel (openerp/model

2019-05-26 15:50发布

问题:

I want to override a function called 'user_has_groups' in the class from the file openerp/models.py (line no 1365)

I tried the code from this post and this question

from openerp.models import BaseModel
def my_user_has_groups(self, cr, uid, groups, context=None):
    #my code

BaseModel.user_has_groups = my_user_has_groups

But it results the following error.

TypeError: my_user_has_groups() takes at least 4 arguments (2 given)

and also i tried this line

BaseModel.user_has_groups = lambda cr, uid, groups, context: my_user_has_groups(cr, uid, groups, context)

It results the following error

QWebException: <lambda>() got multiple values for keyword argument 'groups'

I also tried the code from this post. But it doesn't work.

How I can achieve this? Please enlighten me.

回答1:

I solved this in another way around, which is given below

from openerp import models, api
class BaseModelExtend(models.AbstractModel):
   _name = 'basemodel.extend'

   def _register_hook(self, cr):            
      @api.cr_uid_context
      def user_has_groups(self, cr, uid, groups, context=None):
         #My code
      models.BaseModel.user_has_groups = user_has_groups
      return super(BaseModelExtend, self)._register_hook(cr)


回答2:

from openerp.models import BaseModel,api

@api.guess
def my_user_has_groups(self, cr, uid, groups, context=None):
    # your code

BaseModel.user_has_groups = my_user_has_groups