Search code examples
odooodoo-8openerp-8odoo-9

Assign default value to a Many2one field for specific user group in odoo


I am assigning a default value to analytic distribution field in account.invoice.line by below code

        def _get_default_account(self, cr, uid, context=None):
            res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','LAL')], context=context)
            return res and res[0] or False

        _defaults = {
           'analytics_id': _get_default_account,
           }

but now i want to set default value for specific group of user. I want to set one value for a group and other value for other group. Means I want to set two different default values for different users. Someone please give me some idea about it. I'll be very thankful...


Solution

  • Easiest way to approach that is using has_group method. You should do it like:

    if self.env['res.users'].has_group('base.group1'):
        res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','LAL')], context=context)
        return res and res[0] or False
    elif self.env['res.users'].has_group('base.group2'):
        res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','SAS')], context=context)
        return res and res[0] or False
    

    And so on and so forth.