_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,
}
_defaults
here is just a name (bound to adict
object), there's nothing Python-specific.lambda
is documented in Python's doc : docs.python.org/2/reference/expressions.html#lambda – bruno desthuilliers