Odoo Help


What is 'better': search or browse?

René Schuster
on 7/2/14, 5:17 AM 2,530 views

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.




Ludo - 21South

--Ludo - 21South--
| 2 2 5
Zwijndrecht, Netherlands
--Ludo - 21South--


Ludo - 21South
On 7/2/14, 6:01 AM

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:

    if partner.active:

         print "It is active"

Thx for the reply. Of course often you can't choose between both options. But I#m not sure you got my first example right. Its not just returning the already avaible ids. I've updated my question with an example.

René Schuster
on 7/2/14, 6:22 AM

Aha, now I see. Well in that case, part of my point still stands. If you need just the ids and no more information, the simple search should do the trick. If you know you will be using other information of either project or task (as reference to your example) you will need the browse. Speedwise is should not make much of a difference, unless you are talking about millions of records simultaneously.

21south, Ludo - 21South
on 7/2/14, 6:30 AM

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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/2/14, 5:17 AM
Seen: 2530 times
Last updated: 3/16/15, 8:10 AM