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

What is 'better': search or browse?

By
René Schuster
on 7/2/14, 5:17 AM 1,474 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:
        id_list.append(o2m.id);

or:

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?

EDIT:

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.

 

Thanks.

1

Ludo - Neobis

--Ludo - Neobis--
2387
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--

Developer

Ludo - Neobis
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.

Ludo - Neobis
on 7/2/14, 6:30 AM

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/2/14, 5:17 AM
Seen: 1474 times
Last updated: 3/16/15, 8:10 AM