From new version Odoo 8.0 onwards we are using new api in python coding. But most of the people are unaware of its exact usage. They will use new API in unwanted places. Also, new API provides a lot of functionalities and simple methods to achieve validation and triggering the function call. So now we can spend some time for a clear discussion about new API in odoo.
Method & Decorator
In old we use “cr,cursor,,user,user_id,id,ids,context”. But now we can avoid these things from our coding. We just need to import API. So we can include the following line from the beginning of our code.
V10, V11: - from odoo import api
V8, V9 :- from openerp import api
Here self is represented as a current record. We can use @api.one to avoid single tone error. It updates only one record.
self.name = self.ref_name
self.name = str(self.licence_plate) + ' / ' + str(self.ref_name)
Here self represented as current recordset. Here we can update multiple records.
It will convert old API calls to new API signature. It is more helpfull to migrating code. We can easily update create and write functions which are coded in old API with using @api.model.
def process_unsub(self, tracking_email, metadata):
return self._process_status(tracking_email, metadata, 'unsub', 'unsub')
This decorator function will be called on creating, write and unlink actions. We can include validation through this decorator. It will check the fields which are specified in @api.constrains parameters. So we can give any validation for our fields.
if self.range_start > self.range_stop:
raise exceptions.ValidationError("Start range should be less than stop range")
This will trigger the call to decorated function if there are any changes happened in specified fields due to orm or changes in form.
for email in self:
email.date = fields.Date.to_string(
This will trigger a call to decorated function if there are any changes happened in specified fields due to changes in form.
self.price = self.name.price
We can use env for search. Also, we can avoid cr, uid, the context from search.
invoice_ids = self.env['account.invoice'].search([('origin', '=', self.name)])
We can use env to browse the model. Also, we can avoid cr, uid, the context from browse.
stage = self.env['hr.recruitment.stage'].browse(stage_id)
We can use search_read to return a list of dict values.
return self.search_read(, order='create_date DESC', limit=limit)
It returns the count of search objects.
has_tasks = self.env['project.task'].search_count([('project_id', 'in', projects.ids)])
Here it returns the count of task which are under in specified project
Get Current User
We can use self.env.user to get current user from python.
user = self.env.user
Also, we can use uid to get current user from XML.
<filter string="My" domain="[('user_id', '=', uid)]"/>
Get Records Using XML
To get records from XML we can use self.env.ref.
category = self.env.ref('hr_recruitment.categ_meet_interview')
We can use self.sudo() to get admin power. Also, we can mention the user id into sudo to get other user's access as follow.
applicants = self.sudo().browse(ids)
applicants = self.sudo(user.id).browse(ids)