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语言的知识,发现写了这么久的代码,居然所有的知识点都在自己的脑袋里.这可不好,万一老了呢.... 接下来的几天里,会以文字的形式,将这些知识整理出来,分享给大家. 想要看看英文 ...
随机推荐
- [转]nginx简易教程
安装 nginx官网下载地址 发布版本分为 Linux 和 windows 版本. 也可以下载源码,编译后运行. 从源代码编译 Nginx 把源码解压缩之后,在终端里运行如下命令: $ ./confi ...
- Erlang学习记录:转义
转义 转义序列 含义 整数编码 \b 退格符 8 \d 删除符 127 \e 换码符 27 \f 换页符 12 \n 换行符 10 \r 回车符 13 \s 空格符 32 \t 制表符 9 \v 垂直 ...
- 数论+线性dp——cf1174A
直接推公式没有推出来 看了题解才会做.. 首先能够确定前面几个数的gcd一定是2^j * 3^k, 其中k<=1 那么可以用dp[i][j][k]来表示到第i位的gcd是2^j*3^k f(j, ...
- 使用JDK自带功能,实现一个简单的Web Service接口发布
万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...
- 08_springboot2.x自定义starter
概述 starter:启动器 1.这个场景需要使用到的依赖是什么? 2.如何编写自动配置 规则: @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指 ...
- Django2.0+小程序技术打造微信小程序助手✍✍✍
Django2.0+小程序技术打造微信小程序助手 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题 ...
- java_Set接口
/** * Set接口:extends Collection接口 * 不重复性 * 无序 * * java.util.HashSet: * 实现Set接口,不保证set的迭代顺序,无序集合 * 底层是 ...
- spark jdk8 单词统计示例
在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...
- Codeforces Round #478 Div2 975A 975B 975C 975D
A. Aramic script 题目大意: 对于每个单词,定义一种集合,这个集合包含且仅包含单词中出现的字母.给你一堆单词,问有多少种这种集合. 题解: 状压,插入set,取size #in ...
- 内核下枚举进程 (二)ZwQuerySystemInformation
说明: SYSTEM_INFORMATION_CLASS 的5号功能枚举进程信息.其是这个函数对应着ring3下的 NtQuerySystemInformation,但msdn上说win8以后ZwQu ...