Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

when to use new api decorators??????

By
jhony
on 3/11/15, 1:22 AM 4,633 views

i'm really confused about the decorators in new api....

if anyone is clear about this concept please correct me because currently i'm using this when...

@api.model > method not use ids and no recordset operation done in method...

@api.one > don't know exactly

@api.multi > when method do operaton with multiple records..

if i'm wrong or incorrect please correct my knowledge.. :)

thanks in advance 

 

Thanks Mansi for your useful answer..., but i'm still confused with @api.model in definition what it means by "content is not relevant " ?? can you please explain?!

jhony
on 3/11/15, 6:31 AM

ok thnaks mansi now it's more clear to me. and yes you can upvote question if it is useful...

jhony
on 3/12/15, 1:32 AM
3

Yenthe

--Yenthe--
6676
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 3/11/15, 3:47 AM

If I am not mistaking it is very easy and clear.
The @api.one is specific for one record and can not be used for multiple records. It automaticly loops on records of a Recordset for you. 
@api.many is for multiple records, where you can loop through it etc. It will be the current Recordset without the itiration. So any looping would need to be programmed by yourself, for whatever you want
@api.model is basicly a converter from the old API to the new API. (Odoo V7 -> Odoo V8) Its for migrating code.
@api.depends will trigger the call to the decorated function if any of the fields specified in the decorator is altered by ORM or changed in the form..

An example of @api.one:

@api.one def _compute_name(self):
      self.name = str(random.randint(1, 1e6))

An example of @api.multi:

@api.multi def subscribe(self):
     for session in self.session_ids:
         session.attendee_ids |= self.attendee_ids
     return {}

An example of @api.decorator:

@api.returns('res.partner')
def afun(self):
    ...
    return x # a RecordSet

An example of @api.model:

@api.model
def afun(self):
    pass

An example of @api.depends:

@api.depends('name', 'an_other_field')
def afun(self):
     pass

You can read more about all the decorators and methods here: http://odoo-new-api-guide-line.readthedocs.org/en/latest/decorator.html

4
Mansi Kariya (mka)
On 3/11/15, 2:42 AM

Hello Jhony,

@api.one:  Decorate a record-style method where self is expected to be a singleton instance. The decorated method automatically loops on records, and makes a list with the results. In case the method is decorated with @returns, it concatenates the resulting instances. Such a method:

@api.one

def method(self, args): return self.name

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)

names = recs.method(args)

names = model.method(cr, uid, ids, args, context=context)

 

@api.multi: Decorate a record-style method where self is a recordset. The method typically defines an operation on records. Such a method:

@api.multi

def method(self, args):

...

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)

recs.method(args)

model.method(cr, uid, ids, args, context=context)

 

@api.model: Decorate a record-style method where self is a recordset, but its contents is not relevant, only the model is. Such a method:

@api.model

def method(self, args):

...

may be called in both record and traditional styles, like:

# recs = model.browse(cr, uid, ids, context)

recs.method(args)

model.method(cr, uid, args, context=context)

For more details, Go through Doc.

Hope this will help you.

@api.model decorator will convert old API calls to decorated function to new API signature. It allows to be polite when migrating code. And also if you see example when you override orm create method, @api.model is used where id is not used or you can understand not created yet as id did'n generated for that record.

Mansi Kariya (mka)
on 3/11/15, 8:06 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 3/11/15, 1:22 AM
Seen: 4633 times
Last updated: 7/2/16, 12:07 AM