method-and-deceorator.png
Blogger_636313953512117529.jpg
By: Jesni

Method and decorator

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

 

@api.one

 

Here self is represented as a current record. We can use @api.one to avoid single tone error. It updates only one record.

 

@api.one

def complete_name_compute(self):

    self.name = self.ref_name

    if self.licence_plate:

        self.name = str(self.licence_plate) + ' / ' + str(self.ref_name)


@api.multi

 

Here self represented as current recordset. Here we can update multiple records.

}

@api.multi

def action_create_diagnosis(self):

    self.signal_workflow('create_diagnosis')


@api.model

 

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.

 

@api.model

def process_unsub(self, tracking_email, metadata):

    return self._process_status(tracking_email, metadata, 'unsub', 'unsub')

@api.constrains()

 

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.

 

 

 

@api.constrains('range_start', 'range_stop')

def check_range(self):

    if self.range_start > self.range_stop:

        raise exceptions.ValidationError("Start range should be less than stop range")

@api.depends()

 

This will trigger the call to decorated function if there are any changes happened in specified fields due to orm or changes in form.

 

@api.depends('time')

def _compute_date(self):

    for email in self:

        email.date = fields.Date.to_string(

            fields.Date.from_string(email.time))

@api.onchange()

 

This will trigger a call to decorated function if there are any changes happened in specified fields due to changes in form.

 

@api.onchange('name')

def onchange_name(self):

    self.price = self.name.price

 

search

 

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)])

 

browse

 

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)

 

search_read

 

We can use search_read to return a list of dict values.

 

    return self.search_read([], order='create_date DESC', limit=limit)

 

search_count

 

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')

Changing User

 

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)




Comments

0


Leave a comment