Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

I cant understand method of cosde.

By
huongcute
on 7/29/14, 12:11 AM 2,044 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

2

Remya

--Remya--
5813
| 7 8 8
Thiruvananthapuram, India
--Remya--

Working as an Senior ODOO developer. http://in.linkedin.com/pub/remya-r/69/a23/298

Remya
On 7/29/14, 1:23 AM

 _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!'))"

 

0

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/29/14, 12:11 AM
Seen: 2044 times
Last updated: 3/16/15, 8:10 AM