ORM API

Methods defined on a model are executed on a recordset, and their self is a recordset:

class AModel(models.Model):
_name = 'a.model'
def a_method(self):
# self can be anywhere between 0 records and all records in the
# database
self.do_operation()

Iterating on a recordset will yield new sets of a single record (“singletons”), much like iterating on a Python string yields strings of a single characters:

def do_operation(self):
print self # => a.model(1, 2, 3, 4, 5)
for record in self:
print record # => a.model(1), then a.model(2), then a.model(3), ...

Field access

Recordsets provide an “Active Record” interface: model fields can be read and written directly from the record, but only on singletons (single-record recordsets). Setting a field’s value triggers an update to the database:

>>> record.name
Example Name
>>> record.company_id.name
Company Name
>>> record.name = "Bob"

Trying to read or write a field on multiple records will raise an error.

Accessing a relational field (Many2one, One2many, Many2many) always returns a recordset, empty if the field is not set.

Leave a Reply

Your email address will not be published. Required fields are marked *

Our Clients

ir_attachment_138
ir_attachment_139
ir_attachment_137
ir_attachment_135
ir_attachment_134
ir_attachment_133
Powered by DevIntelle Consulting Service Pvt Ltd.
Copyrights © 2016 DevIntelle Consulting Service Pvt Ltd. All rights reserved.