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.

3

Correct way: Browse vs Search ?

By
Mario Arias Badila
on 4/10/13, 4:00 AM 9,328 views

Hi,

I have found in official addons that even though a record has a reference to other records (like one2many), and they already have a browse record with the required information, the programmer still uses the search function to get the list of ids from the database.

Example:

Each hr_employee has a field contract_ids, that is a one2many relationship to hr_contract.

If I already have

   employee_data = employee_obj.browse( blah, blah, blah...)

and I want to access the list of contracts for that employee, what is the right way to get it:

a) Use the list I already have on employee_data.contract_ids and get the information from there,

or

b) Execute a search on contract object looking for contracts related to the employee, like:

   contract_ids = obj_contract.search(cr, uid, [('employee_id','=',emp.id),], context=context)

I would think option a), but I have found that in most cases option b) is used in official addons, so I guess there is a reason for that...

What are cons and pros of each aproach ?

The advantage of first method is that you have an access directly to browse_records. In the second way you must call a browse() method on obj_contracts with your contract_ids to access to their attributes. But I don't know about performances. You must know that many of addons was written in old versions of OpenERP. Maybe browse method was not so efficiently than know...?

Clément THOMAS
on 4/10/13, 4:16 AM
1
Serge
On 4/7/14, 12:54 PM

if you already have that, and no realy need, replace it by a read

employee_data = employee_obj.browse( cr, uid, ids, context=context)

like

for employee in employee_obj.read( cr, uid, ids, ['contract_ids'], context=context):
    #So in employee['contract_ids'], you have a list of all contract id for this employee
1
Turkesh Patel (tpa)
On 4/10/13, 4:27 AM

option a is right way as option b dose not make any sense. we can say it is mistake of coding in IMO.

--> there is one big plus point of option a: As it will avoids search on object so it will good if you have lots of records in db.

option 1 , is a no sense, browse will read ALL field in db, so performance drop, trace the code, if you have 80 fields in the table, openerp will create a select query for 80 fields. Always avoid Browse, browse is for lazy programmer.

Serge
on 4/7/14, 12:55 PM

what if you have 10-20 lakh records and you fire search query then it will take less time than browse of 80 fields????? in this case search makes no sense.

Turkesh Patel (tpa)
on 4/8/14, 2:41 AM

No need a search if you already have the IDS, if you do a browse, mean you have IDS, so do a READ not a BROWSE.

Serge
on 4/8/14, 10:59 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

2 follower(s)

Stats

Asked: 4/10/13, 4:00 AM
Seen: 9328 times
Last updated: 11/28/16, 8:41 AM