Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
Two snippets to get the same list of ids:
id_list = ;
for obj in self.browse(cr, uid, ids, context=None):
for o2m in obj.o2m_field_ids:
o2m_obj = self.pool.get('o2m.object');
id_list = o2m_obj.search(cr, uid, [('m2o_field_id','in',ids)]);
Which is faster? 'better'? cleaner? more pythonic? more readable? better style?
An actual example would be to collect all tasks of one or more projects:
I could loop over the projects and collect the tasks (browse the projects), or search for all tasks that belong to the given projects (search in tasks).
I think this can make differences in performance, etc.
Well, I don't think you should use both for the same purposes. If you want just a list of ids to push around, use the search. It will always give you back just ids in a list. If you want to do something with the fields of these records as well, you use the browse.
The top example is very counter-intuitive as well. You already have the ids; self.browse(cr, uid, ids, context=None), so why bother going trough the objects to find that the result of the browse and for-loop is the exact same as the ids you are passing already?
So for example if you need all the partners that are customers:
partner_obj = self.pool.get('res.partner')
partner_ids = partner_obj.search(cr, uid, [('customer','=',True)])
and if you need more information of those partners, feed that list into a browse:
partners = partner_obj.browse(cr, uid, partner_ids)
for partner in partners:
print "It is active"
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 platform 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.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 7/2/14, 5:17 AM|
|Seen: 1600 times|
|Last updated: 3/16/15, 8:10 AM|