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), ...
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.