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