ODOO 新API修饰符
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
ODOO 新API修饰符的更多相关文章
- 【转】odoo 新API装饰器中one、model、multi的区别
http://blog.csdn.net/qq_18863573/article/details/51114893 1.one装饰器详解 odoo新API中定义方式: date=fields.Date ...
- Odoo 新 API 概述
__all__ = [ 'Environment', 'Meta', 'guess', 'noguess', 'model', 'multi', 'one', 'cr', 'cr_context', ...
- Odoo 二次开发教程(五)-新API的介绍与应用
[关于odoo新API的介绍,Internet上资料很少,或者不够完整详实,这会对初学者造成很大的困惑,本篇的目的就是希望能帮助新手了解新API的大概] odoo 新api的实现是借助于python装 ...
- Java开发笔记(五十)几种开放性修饰符
前面介绍子类继承父类的时候,提到了public(公共)和private(私有)两个修饰符,其中public表示它所修饰的实体是允许外部访问的:而private表示它所修饰的实体不允许外部访问,只能在当 ...
- .NET4.5新特性async和await修饰符实现异步编程
开篇 每一个版本的.net都会引入一些新的特性,这些特性方便开发人员能够快速实现一些功能.虽然.net版本一直在更新,但是新版本对旧版本的程序都是兼容的,在这一点上微软做的还是非常好的.每次学一个新内 ...
- php新版本号废弃 preg_replace /e 修饰符
近期serverphp版本号升级到了 5.6 发现出了非常多警告 preg_replace(): The /e modifier is deprecated, use preg_replace_ca ...
- JavaSE(三)之static、final、abstract修饰符
一.static修饰符 1.1.static变量 在类中,使用static修饰的成员变量,就是静态变量,反之为非静态变量. 静态变量和非静态变量的区别 静态变量属于类的,&quo ...
- Java基础知识回顾之二 ----- 修饰符和String
前言 在上一篇中,回顾了Java的基本数据类型 ,这篇就来回顾下Java中的一些修饰符以及String. 修饰符介绍 Java修饰符主要分为两类: 访问修饰符 非访问修饰符 其中访问修饰符主要包括 p ...
- C语言常用修饰符
前言 这两天在梳理自己C语言的知识,发现写了这么久的代码,居然所有的知识点都在自己的脑袋里.这可不好,万一老了呢.... 接下来的几天里,会以文字的形式,将这些知识整理出来,分享给大家. 想要看看英文 ...
随机推荐
- Linux命令速查手册(第2版)学习
第1章.需要了解的命令行相关事项 表1-1 如何在文件名字符中使用特殊字符 字符 建议 / 绝不使用.不能转义 \ 必须转义.避免使用 _ 绝不能作为文件或目录名的第一个字符 [] 必须转义.避免使用 ...
- 「题解」:$Simple$
问题 A: $Simple$ 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 题解 不算数学的数学题?? 直接枚举会重.$60%$两种算法:1.无脑$vis$数组记录.2.$ ...
- kubernetes忘记token或者token过期怎么加入k8s集群
1.先查看token是否还可用 [root@hadoop01 ~]# kubeadm token list 1.1) 还在则获取ca证书sha256编码hash值,不在则进行2操作 openssl x ...
- (转)Android在子线程中更新Activity中UI的方法
转:http://blog.sina.com.cn/s/blog_3fe961ae0100mvc5.html 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处 ...
- memcpy函数实现中的优化
今天浏览Google面试题的时候,有看到一个memcpy的实现,以及如何去优化memcpy. 我对memcpy的实现的记忆就是,拷贝的时候需要从后往前拷贝,为何防止内存重叠. 但是如果去优化它我没有想 ...
- 现金贷平台下载量TOP100 涉逾30家P2P
一.什么是现金贷,现状如何 那么什么是现金贷呢?在笔者看来,狭义的现金贷主要是指基于互联网等技术手段的小额现金贷款,广义的现金贷可以包括任何以小额现金和存款为标的进行借贷的行为,是一种无担保.无抵押. ...
- Java-Maven-pom.xml-project-packaging:packaging(war/jar)
ylbtech-Java-Maven-pom.xml-project-packaging:packaging(war/jar) 1.返回顶部 1.packaging 1.1 war <!-- 打 ...
- PAT甲级——A1133 Splitting A Linked List【25】
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- java-day04
IntelliJ快捷键 导入包 alt + enter 删除光标所在行 ctrl + y 复制光标所在行 ctrl + d 格式代码 ctrl + alt + l 单行注释 ctrl + / 多行注释 ...
- springboot新手脱坑之无法下载依赖包
1. Apache maven 3.39配置 1.环境变量自己配置, 2.配置阿里云镜像和本地仓库 <localRepository>D:\Apache\maven\repository& ...