Monday 22 September 2014

Odoo New API: Recordsets

06:35

Share it Please

Recordset:

A recordset is:
  • An ordered collection of records.
  • One concept to replace:
              - browse records,
              - browse lists,
              - browse nulls.
  • An instance of the model's class.

1. The recordset as a collection:

It implements a sequence and set operations:

Operation Supported:

RecordSet also support set operations you can add, union and intersect, ... recordset:

1. Addition:

recordsetA + recordsetB: Returns the concatenation of two recordsets.

2. Substration:

recordsetA - recordsetB: Return the recordset of all the records in 'recordsetA' that are not in 'recordsetB'.

3. Intersection:

recordsetA & recordsetB: Return the intersection of two recordsets. Note that recordset order is not preserved.

4. Union:

recordsetA | recordsetB: Return the union of two recordsets. Note that recordset order is not preserved.

5. Equivalent:

recordsetA == recordsetB: Test whether two recordsets are equivalent (up to reordering).

...etc

2. The recordset as a record:

It behaves just like former browse records:

    print partner.name
    print partner['name']
    print partner.parent_id.company_id.name

Except that updates are written to database:

    partner.name = 'Agrolait'
    partner.email = 'info@agrolait.com'
    partner.parent_id = ...   # another record

If len(partners) > 1, do it on the first record:

    print partners.name           # name of first partner
    print partners[0].name

    partners.name = 'Agrolait'    # assign to first partner
    partners[0].name = 'Agrolait'

If len(partners) == 0, return the null value of the field:

    print partners.name        # False
    print partners.parent_id   # Empty recordset
    partners.name = 'Foo'      # Error

3. The recordset as an instance:

Methods of the model's class can be invoked on recordsets:

# calling convention: leave out cr, uid, ids, context
@api.multi
def write(self, values):
    result = super(C, self).write(values)
 
    # search returns a recordset instead of a list of ids
    domain = [('id', 'in', self.ids), ('parent_id', '=', False)]
    roots = self.search(domain)
 
    # modify all records in roots
    roots.write({'modified': True})
 
    return result

The missing parameters are hidden inside the recordset.

Record creation in cache only:

You can create a new record with following method:

@api.model
def new(self, values={}):
    """ new([values]) -> record """

Return a new record instance attached to the current environment and initialized with the provided 'value'. The record is not created in database, it only exists in memory.

New record is used to compute default values and perform onchanges.

Example:

record = self.new(values)

This will creates a new record with values, and attach 'self' to it.

Comment With:
OR
The Choice is Yours!

2 comments:

  1. Enpersol is an Odoo consultants Company in indore proficient in business process reenginerring and optimization. Enpersol is an Odoo partners Company provide best Odoo services in pune.

    ReplyDelete
  2. Nice and Impressive Blog!Thanks for sharing...

    Odoo Development Services

    ReplyDelete