In Odoo8, the API interface is divided into traditaional style and record style two types:
Traditional style refers to the type that we use in 7.0, the Def (self,cr,uid,ids,context)-style syntax.
The style of the record style 8.0 and later versions of the refinement parameters, only the self and args are preserved, as Def (Self,args)
Method and decorator
New decorators is just mapper around the new API. The decorator is mandatory as WebClient and HTTP controller is not compliant with the new API.
api
Namespace decorators would detect signature using variable name and decide to match old signature or not.
Recognized variable names is:
cr, cursor, uid, user, user_id, id, ids, context
@api. Returns
This decorator guaranties unity of returned value. It would return a RecordSet of specified model based on original returned value:
Ensure that the return value is uniform. Returns a recordset that specifies the model based on the original return value
@api. Returns(' Res.partner ')afun(self# a RecordSet
And if an old API function calls a new API function it would automatically convert it into a list of IDs
All decorators inherits from this decorator to upgrade or downgrade the returned value.
The usage is primarily used to specify the format of the return value, which accepts three parameters, the first is the model of the return value, the second is a backward-compatible method, and the third is an upward-compliant method
The first argument is the object itself, with ' self ', and if it is another object, write the other object names such as: @api. Returns (' Ir.ui.view ')
@api. One
This decorator loops automatically on Records of the RecordSet for you. Self was redefined as current record:
The one decorator automatically iterates through the recordset, redefining self as the current record. "For a method similar to Def funct (Self,cr,uid,ids,context) in the V7 version, you can use the Api.one adorner instead of the V8 notation. 】
@api. Oneafun(self self.') Toto '
Note
Caution:the returned value is the put in a list. This isn't always supported by the Web client, e.g. On button action methods. In this case, you should use @api.multi
to decorate your method, and probably call Self.ensure_one () in the method Definition.
Note that the return value is a list. The decoration may not be supported by the Web client. This should be done with the @api.multi modifier function, which may also require a self.ensure_one ()
@api. Multi
Self'll be is the current RecordSet without iteration. It is the default behavior:
Self is the current recordset. No traversal required
@api. Multiafun(self)
@api. Model
This decorator would convert old API calls to decorated function to new API signature. It allows to is polite when migrating code.
The adornment transforms the old API function into a new API function symbol with a decorator, so that the code can be migrated smoothly "for a method similar to Def funct (Self,cr,uid,args,context) in V7. 】
1 @api. Model afun(self): Pass
2 Definition Columns
Method definition:
@api.modeldef _get_lang (self): langs = self.env[' Res.lang '].search ([]) return [(Lang.code,lang.name) for Lang in langs]
@api. Constrains
This decorator would ensure that decorated function would be is called on Create, write, unlink operation. If A constraint is met the function should raise a openerp.exceptions.Warning with appropriate message.
The decoration ensures that the modified function is called when it is create, write, unlink. When the constraint is satisfied, the function should raise the corresponding exception warning message
@api.constrains (' age ')
Def_check_age (self):
If self.age<16:
Raise ValueError (_ (' age must is older than 16 '))
@api. Depends
This decorator would 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:
When any of the dependent fields change (ORM or Form), trigger the function to execute
@api. Depends(' name ' 'An_other_field ')afun(selfpass
Note
When you redefine depends you have the redefine all @api. Depends, so it loses some of he interest.
View Management
One of the great improvement of the new API is, the depends is automatically inserted to the form for your in a simp Le. Worry about modifying views anymore.
We know that the function field in 7.0 is not stored by default and needs to be stored using the store parameter. V8 all fields with the COMPUTE parameter are not stored by default, the store parameter becomes a Boolean type, and the store's trigger function is no longer available. The field using depends here is equivalent to the trigger in V7, and when the dependent fields change, the function here is triggered to update the database. However, if the field depends depends on is not stored, it will still not be triggered.
@api. onchange
This decorator would trigger the call to the decorated function if any of the fields specified in the decorator is changed In the form:
@api . ( ' fieldx ' ) def do_ Stuff (selfif self fieldx == x: self fieldy = ' Toto '
In previous, sample self corresponds to the record currently edited on the form. When in On_change context all work was done in the cache. So you can alter the RecordSet inside your function without being worried about altering database. That ' s the main difference with@api.depends
At function return, differences between the cache and the RecordSet is returned to the form.
In the example, self is the record in the form, and when the on_change context, all the work is done in the cache, so you can modify the recordset directly in the function without worrying about modifying the database, which is the biggest difference from the depends. Returns the differences in the cache and recordset to the form when the function returns.
View Management
One of the great improvement of the new API is, the onchange is automatically inserted to the form for your in a sim Ple. Worry about modifying views anymore.
Warning and Domain
To change domain or send a warning just return the usual dictionary. Be careful does not have @api.one
the use of in this case as it would mangle the dictionary (put it in a list, which are not supported by the Web client).
@api. noguess
This decorator prevent new API decorators to alter the output of a method
ODOO new API modifier