Odoo8中,API接口分為traditaional style和record style兩種類型:
traditional style指的就是我們在7.0中使用的類型,def(self,cr,uid,ids,context)式的語法.
record style 8.0及以后版本精簡化參數后的風格,只保留了self和args,形如def(self,args)
Method and decorator
New decorators are just mapper around the new API. The decorator are mandatory as webclient and HTTP controller are not compliant with new API.
api
namespace decorators will detect signature using variable name and decide to match old signature or not.
Recognized variable names are:
cr, cursor, uid, user, user_id, id, ids, context
@api.returns
This decorator guaranties unity of returned value. It will return a RecordSet of specified model based on original returned value:
保證返回值的統一。將返回基於原始返回值指定model的記錄集
@api.returns('res.partner') def afun(self): ... return x # a RecordSet
And if an old API function calls a new API function it will automatically convert it into a list of ids
All decorators inherits from this decorator to upgrade or downgrade the returned value.
用法主要是用來指定返回值的格式,它接受三個參數,第一個為返回值的model,第二個為向下兼容的method,第三個為向上兼容的method
第一個參數如果是對象本身,以'self',如果是其他對象,則寫其他對象名如:@api.returns('ir.ui.view')
@api.one
This decorator loops automatically on Records of RecordSet for you. Self is redefined as current record:
one裝飾符自動遍歷記錄集,把self重新定義成當前記錄。【對於v7版本中類似於 def funct(self,cr,uid,ids,context)的方法可以用api.one裝飾器改用v8寫法。】
@api.one def afun(self): self.name = 'toto'
Note
Caution: the returned value is put in a list. This is not always supported by the web client, e.g. on button action methods. In that case, you should use @api.multi
to decorate your method, and probably call self.ensure_one() in the method definition.
注意,返回值是一個list. web client有可能不支持該裝飾。這時應該用@api.multi修飾函數,函數中可能還需要條用self.ensure_one()
@api.multi
Self will be the current RecordSet without iteration. It is the default behavior:
self就是當前記錄集。不需要遍歷
@api.multi def afun(self): len(self)
@api.model
This decorator will convert old API calls to decorated function to new API signature. It allows to be polite when migrating code.
該裝飾會將舊的API函數轉換成帶有裝飾符的新API函數符號,使得代碼可以平滑遷移【即適用於v7中類似於 def funct(self,cr,uid,args,context)的方法。 】
1 @api.model def afun(self): pass
2 定義columns
langs = fields.Selection(string="Lang",selection="_get_lang")
方法定義:
@api.model def _get_lang(self): langs = self.env['res.lang'].search([]) return [(lang.code,lang.name) for lang in langs]
@api.constrains
This decorator will ensure that decorated function will be called on create, write, unlink operation. If a constraint is met the function should raise a openerp.exceptions.Warning with appropriate message.
該裝飾確保被修飾的函數在create, write, unlink時被調用。當約束條件滿足時,函數應該raise 相應的異常警告消息
@api.constrains('age')
def_check_age(self):
if self.age<16:
raise ValueError(_('Age must be older than 16'))
@api.depends
This decorator 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:
依賴的任一字段變化時(ORM or Form),觸發該函數執行
@api.depends('name', 'an_other_field') def afun(self): pass
Note
when you redefine depends you have to redefine all @api.depends, so it loses some of his interest.
View management
One of the great improvement of the new API is that the depends are automatically inserted into the form for you in a simple way. You do not have to worry about modifying views anymore.
我們知道7.0中function字段是默認不存儲的,需要使用store參數進行存儲。v8當中所有帶有compute參數的字段默認都不會存儲,store參數也變成了boolean類型,不再提供store的觸發函數。這里使用depends的字段就相當於v7當中的trigger,當依賴的字段發生改變時,會觸發這里的函數對數據庫進行更新。但如果depends依賴的字段也沒有存儲,那么依舊不會觸發。
@api.onchange
This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form:
@api.onchange('fieldx') def do_stuff(self): if 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 is done in the cache. So you can alter 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 will be returned to the form.
例子中self是指form中的記錄,當在on_change上下文時,所有的工作都是在緩存中進行,所以你可以在函數中直接修改記錄集而不用擔心會修改數據庫, 這是和depends的最大區別。函數返回時將緩存和記錄集中的差異返回給form.
View management
One of the great improvement of the new API is that the onchange are automatically inserted into the form for you in a simple way. You do not have to worry about modifying views anymore.
Warning and Domain
To change domain or send a warning just return the usual dictionary. Be careful not to use @api.one
in that case as it will mangle the dictionary (put it in a list, which is not supported by the web client).
@api.noguess
This decorator prevent new API decorators to alter the output of a method