ORM Methods in OpenERP
5 March, 2021 by
ORM Methods in OpenERP
Administrator

Every object in OpenERP is based on OSV i.e Object Service and this service implements full Object-Relational Mapping enabling developers not to worry for the simple SQL operations.

In OpenERP, we have such ORM methods that are very useful. Here are the few basic methods:

    1. read(cr, uid, ids, fields=None, context=None): returns a list of dictionaries with field values.
  1. res = self.read(cr, uid, [5,6],[‘name’, ‘partner_id’])
  2. print ‘Partner:’, res[0][‘partner_id’]
  1. write(cr, uid, ids, values, context=None): updates a record with provided values.
    1. for line in self.browse(cr, uid, ids, context=context):
    2. if set_total:
    3. self.pool.get(‘add.penalty’).write(cr, uid, [line.id], {‘name’: line.name})
  2. search(cr, uid, values, args, offset=0, limit=None, order=None, context=None, count=False): returns list of ids (tuple) on search criteria
    1. seq_id = self.pool.get(‘ir.sequence’).search(cr, uid, [(‘name’,’=’,’Sales Journal’)])[0]
  3. copy(cr, uid, id, defaults, context=None): duplicates a record with default values
    1. def copy(self, cr, uid, id, default=None, context=None):
    2. default = default or {}
    3. default.update({‘invoice_ids’ : []})
    4. return super(res_partner, self).copy(cr, uid, id, default, context)
  4. unlink(cr, uid, values, context=None): deletes a record with given id or ids
    1. account_move_obj.unlink(cr, uid, move_ids, context=context)
  5. create(cr, uid, values, context=None): creates a new record with given values and returns the newly created record id
    1. partner_id = self.create(cr, uid,
    2. { ‘name’: ‘XYZ’,
    3. ‘description’ : ‘Contact’,
    4. ‘phone’: 123456,
    5. })
  6. browse(cr, uid, values, context=None): retrieves records with required fields, returns a list of records
    1. invoice = self.pool.get(‘account.invoice’)
    2. cr.execute(“select * from account_invoice where state = ‘open’ and payment_term > 0 and cash_credit=’Credit Memo'”)
    3. invoices = invoice.browse(cr, uid, map(lambda x: x[0], cr.fetchall()) )

For more you can visit OpenERP ORM Methods