0
votes

I have started learning to develop for OpenERP modules under Python language. I have been looking source code examples over internet and try to learn by my own. I have bben going through a Python code and am unable to understand use of _defaults and lambda. Like :

 _defaults = {
        'name': lambda obj, cr, uid, context: '/',
        'state': 'draft',

    }

Plz give a few words to get some know-how on these two .

Hopes for suggestion

Thanks

2
_defaults here is just a name (bound to a dict object), there's nothing Python-specific. lambda is documented in Python's doc : docs.python.org/2/reference/expressions.html#lambdabruno desthuilliers
I am very much aware with use of lambda in python language but I cant understand its need n use within OpenERP development. Like I said earlier the code I mentioned above is also beyond my understanding. Can you please put some light on usage of lambda within Openerp(Python)Arsalan Sherwani

2 Answers

2
votes

_default dictionary is used for setting defaults values of fields given inside it. This values will be displayed by default while creating any new record from UI.

lambda : Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda". This means that you can use a function runtime without defining it with a name, having all its code in a single line. Now, in OpenERP, you can see that functions are having some arguments like cr(database cursor), uid,id etc. Using all this, you can access the database (using OpenERP's ORM methods). So instead of defining a whole function just to set default values, lambda function is a smart choice. For example ,

_defaults = {
    'active': True,
    'type': 'general',
    'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
}

In the code above, company_id can be set to default using lambda that calls browse method of ORM for class 'res.users' to get the id of loggedin user.

You can do it this way also by defining a function,

def _get_default_company(self, cr, uid, context=None):
    company_id = self.pool.get('res.users')._get_company(cr, uid, context=context)
    return company_id        
_defaults = {
    'active': True,
    'type': 'general',
    'company_id': _get_default_company,
}
0
votes

@ArsalanSherwani: Basically we can value assignment without lambda also, lambda should be used only when we want some dynamic or calculated values but if you want assign static value you can directly assign as _default is just a Dict. As you know lambda in line function so instead of that you can call external function inside dict to calculate the value, this all thing is pretty much pythonic only, nothing specific to OpenERP only special thing about _defaults is it set the default value for fields when you can try to create new record i.e.when you click create button in View, it calls the _default_get methods and default_get method in ORM will calculate values from this block and will return the values. So you can change behavior of the _defaults balck values using _defulat_Get also.

@ArsalanSherwani : in your code

lambda obj, cr, uid, context: '/',

  • obj refer to Self Object i.e. that is your current class itself.
  • cr: database cursor object to perform SQL operation on DB :)
  • uid L is current user logged in .
  • context is dict fro pass the contextual values between objects.

this all params are default to any class method derived from osv.osv or osv.Model and this params are global to use so you can use them with method you want.