Reputation: 560
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
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;
sale_order_line.sale_order.partner.email
)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:
product.id
__eq__
so you can go if product_browse_record == other_product_browse_record
and it works.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
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