Tuesday, July 21, 2015

Odoo v8 New API

Hello all,

Its been long time of my presence here. Today, I would like to share few things about Odoo v8 new API which can help us to develop modules easily.

With new API, functions can be execute on a recordset, We consider 'self' as a recordset.

class test_test(models.model):
    _name = 'test.test'
    def method_test(self):

Recordset Operations:

filtered():
records.filtered(lambda l: l.account_id == self.account_id)
sorted():
records.sorted(key=lambda t: t.date)
mapped():
records.mapped('product_id.type') 

Environment variables: 
Remember: cr, uid, context in old API. In new API, we can use it with recordsets, as below:

records.env --> returns object name
records.env.cr --> returns cursor object
records.env.user --> returns logged user id

Environment alternatives:
records.env['res.partner'].sudo().create({'name': 'Priyesh'})
records.env['res.partner'].with_context(partner_id=so.partner_id).create(vals)

ORM Methods:
Consider 'self' as a recordset as below:

self.create({'name': 'Priyesh'})
self.write({'name': 'Priyesh'})
self.browse([1, 2, 3, 4, 5])
self.unlink()

Model definition with old API:
class test(osv.osv):
Model definition with new API:
class test(models.model):
Field definition with old API:
'name': fields.char('Description', size=64, select=True, required=True)
Field definition with new API:
name = fields.Char(string='Reference/Description', index=True, required=True)
name = fields.char(string='Name', default=default_name) --> To compute default value
name = fields.char(string='Name', compute='_compute_name') --> Function field

To allow searching on computed field, set 'search' parameter as below:
name = fields.char(string='Name', compute='_compute_name', search='_search_name')
To allow setting value of computed field, set 'inverse' parameter as below:
name = fields.char(string='Name', compute='_compute_name', inverse='_set_name') 

More coming soon.....

No comments:

Post a Comment