Enable Dark Mode!
By: Sruthi C Nair

How to Use Function Wrappers in Odoo?


In order to extend a function's behavior, wrappers enclose it within another function. Its functionality is available in Python. Make a function call inside a wrapper function inside function b, passing as an argument a function assumed to be function 'a' inside another function assumed to be function 'b'. So the possibility is that we can reuse the code.
def function(n):
This is also similar to
def function(n):
function = wrapper(function)
def button_conform(self,):
   amount = []
   for recs in self:
       for rec in recs.line_ids:
   if sum(amount) > self.petty_limit:
       self.limit = True
   self.write({'state': 'requested', }
def partner_name():
   partner = self.env['res.partner'].search([])
   product = self.env['product.template'].search([])
return partner_name()
def users(*logins):
   """ Decorate a method to execute it once for each given user. """
   def wrapper(func, *args, **kwargs):
       self = args[0]
       old_uid = self.uid
           Users = self.env['res.users'].with_context(active_test=False)
           user_id = {
               user.login: user.id
               for user in Users.search([('login', 'in', list(logins))])
           for login in logins:
               with self.subTest(login=login):
                   self.uid = user_id[login]
                   func(*args, **kwargs)
           self.uid = old_uid
   return wrapper

Wrapper Class in Python

The way to specify the management code of functions and classes is by Decorators.

The callable object is taken from the decorators themself as a form. And that process callable objects. A Class Decorator act similarly to function decorators, but the difference is the end of a class statement; they are run to rebind a class name to a callable. Similarly, when they are later created, a layer of wrapper logic manages instances just after they are created or inserted.

We can able to use directly class objects in a class decorator to manage instead of instance calls–by using nearly the same syntax and very similar coding patterns to increase/modify a class with new methods. And class decorators are strongly related to function decorators, but somewhere logic differs.


Syntactically, class decorators appear just before class statements.

class Class_Name:
inst = Class_Name(50)

This part of the code is equivalent to

class Class_Name:


Class_Name = decorator(Class_Name)

inst = Class_Name(50);

   # decorator accepts a class as
   # a parameter
def decorator(cls):
   class Wrapper:
       def __init__(self, x):
           self.wrap = cls(x)
       def get_name(self):
           # fetches the name attribute
           return self.wrap.name
   return Wrapper
class C:
   def __init__(self, y):
       self.name = y
# its equivalent to saying
# C = decorator(C)
x = C("test")

That’s how we can wrap the functions in python.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message