Skip to Content
Menu
This question has been flagged
2 Replies
24256 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 

 

Avatar
Discard
Author

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?!

Author

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

Best Answer

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

Avatar
Discard
Best Answer

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.

Avatar
Discard

@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.

Related Posts Replies Views Activity
0
Mar 15
2934
2
Jan 16
5036
1
May 25
803
0
Jan 24
1571
2
May 23
8440