This question has been flagged
2 Replies
5319 Views

Hi all !

I dont understand a short code in module Sale :

def _get_default_shop(self, cr, uid, context=None):
        company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
        shop_ids = self.pool.get('sale.shop').search(cr, uid, [('company_id','=',company_id)], context=context)
        if not shop_ids:
            raise osv.except_osv(_('Error!'), _('There is no default shop for the current user\'s company!'))
        return shop_ids[0]

 

I dont know its useful . The specific :

company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id

 

Someone can explain it to help me?? Thanks all

Avatar
Discard
Best Answer

 _defaults = {

               'shop_id': _get_default_shop,

}

the above code is used to select user's shop id. 

company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id

self.pool.get('res.users') --- logically you try to use existing module’s model or dependent model. 

browse(cr, uid, uid, context=context).company_id.id --- browse company_id.id of the user using uid(user's id) in res.users model.

Now we get the company id of the corresponding user in the variable company_id.

shop_ids = self.pool.get('sale.shop').search(cr, uid, [('company_id','=',company_id)], context=context)

self.pool.get('sale.shop') --- logically you try to use existing module’s model or dependent model. 

search(cr, uid, [('company_id','=',company_id)], context=context) --- search the users company id with the value in the above variable company_id in sale.shop model. Both values should equal, if values are not equal then it pop up the error "raise osv.except_osv(_('Error!'), _('There is no default shop for the current user\'s company!'))"

 

Avatar
Discard
Best Answer

Hello Huongcute,

"self.pool.get('res.users') -> this brings the "res_users" object stored in the memory

".browse(cr, uid, uid, context=context)" -> this brings up the browse record of "res_users" for the id as "uid" i.e "1", which means the "Administrator" and as only single id is passed (not a list) under browse record method it returns only single browse record object so,  

self.pool.get('res.users')..browse(cr, uid, uid, context=context) -> returns the single browse record of "res_users" on which it tries to find the attribute "company_id.id". This works only when you have only single browse record, if there are more than one browse record than you have to use the array index i.e [0],[1]... so on, as it returns list of browse record instead on single browse record

 

Regards

Avatar
Discard