Arsalan Sherwani
Arsalan Sherwani

Reputation: 560

what is the purpose of using self.browse in python for openerp development?

Hi I have been working on developing an OpenERP module using Python . I have been going through a source code and having problem understand it. I dont understand the following two lines where self.browse having id,uid as parameters and then usage of tools with functions

  for obj in self.browse(cr, uid, ids, context=context):

           result[obj.id] = tools.image_get_resized_images(obj.image)

Plz give me a little know-how about this. Thanks Hopes for suggestion

Upvotes: 1

Views: 3505

Answers (2)

Adrian Merrall
Adrian Merrall

Reputation: 2499

As in Firebug's answer, you can think of the browse as a read or in simple terms, a SQL select statement but with a few differences. Technically, they represent an instance of the data defined by an ORM model - product.product defines the model (or table), a browse record is a row of data from the table.

Browse takes either a single ID (e.g. 1) and returns a browse record or takes a list of Ids [1,2,3...] and returns a list of browse records.

It does ultimately read from the database but it also does a few other things that a read doesn't;

  1. Caching
  2. Lazy loading to any depth (sale_order_line.sale_order.partner.email)
  3. Handles virtual fields such as functional fields, relational fields (many2one,one2many), related fields.

In the simplest sense, think of it as select * from my_table where id = %s and then assembling an object from the result.

A few things to note:

  1. Browse records always have an ID - e.g. product.id
  2. You can't pass browse records to a client. They lazily load and hold on to a database cursor, if you try you see the "cursor used after being closed" message.
  3. They don't handle browsing missing records very well. Make sure the records you are browsing exist. Normally this isn't something you have to worry about, but if you need to be sure, do a search.
  4. Browse records implement __eq__ so you can go if product_browse_record == other_product_browse_record and it works.
  5. A very common pattern is as you have above:

    for product in product_model.browse(cr, uid, ids, context = context): do something with my product

This is fine 99 times out of 100 but be aware that it will read all the database records in one go and construct a list of the objects. If you have a huge list of IDs, you will use a big chunk of memory.

Upvotes: 1

ifixthat
ifixthat

Reputation: 6295

In Simple words browse is the method which enable the read operation on database table records. browse method Fetch records as objects allowing to use dot notation to browse fields and relations. which bring OpenERP Programming near to OOPs.

OpenERP framework is codded in Python Programming using the ORM and MVC Design Patterns. ORM wraps the use value in Object and allows CRUD operation methods in various methods i.e. URL. read methods is alternate of the browse methods where read return the python list of dict and browse return the list of objects each object is record in database.

So analysis of your code is : this statement for obj in self.browse(cr, uid, ids, context=context): can be devided in to lines. 1. self.browse(cr, uid, ids, context=context) fetching the record(ids) from self(object). 2. for that is looping through the return of the above. so each time loops iter it store the record in obj which is basically db record and as it is record and object it wraps the tablet column values as attribute in side, so you can fetch the field values using obj.field_name

Hope this will help.

Upvotes: 4

Related Questions