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语言的知识,发现写了这么久的代码,居然所有的知识点都在自己的脑袋里.这可不好,万一老了呢.... 接下来的几天里,会以文字的形式,将这些知识整理出来,分享给大家. 想要看看英文 ...
随机推荐
- 配置文件一web.xml
前言 web.xml中标签的加载顺序:<context-param> > <listener> (spring的相关工作) > filter >servlet ...
- 【转载】Abstract Factory Step by Step --- 抽象工厂
抽象工厂是创建型模式的代表,其他的还有单件(Singleton).生成器(Builder).工厂方法(Factory Method)以及原型(Prototype),模式本身没有好坏之分,只有适用不适用 ...
- VPGAME的Kubernetes迁移实践
VPGAME 是集赛事运营.媒体资讯.大数据分析.玩家社群.游戏周边等为一体的综合电竞服务平台.总部位于中国杭州,在上海和美国西雅图分别设立了电竞大数据研发中心和 AI 研发中心.本文将讲述 VPGA ...
- 如何区分无线AP跟无线路由器
无线AP是一个无线网络的接入点,俗称“热点”.主要有路由交换接入一体设备和纯接入点设备,一体设备执行接入和路由工作,纯接入设备只负责无线客户端的接入,纯接入设备通常作为无线网络扩展使用,与其他AP或者 ...
- Android Support 包的作用、用法
1, Android Support V4, V7, V13是什么?本质上就是三个java library. 2, 为什么要有support库?如果在低版本Android平台上开发一个应用程序,而应 ...
- GNU GRUB引导的默认启动项是ubuntu
安装了ubuntu16.04后,GNU GRUB引导的默认启动项是ubuntu,如果希望默认启动项是windows,修改方法如下: step1. 进入Ubuntu系统,打开终端,输入 sudo ged ...
- Android开发 输入法调用学习
方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context. ...
- bootstrap table 实现固定悬浮table 表头并可以水平滚动
在开发项目中,需要将表格头部固定,而且表格大多数情况下是会水平滚动的.项目的css框架是bootstrap 3,故也可以叫做bootstrap table. 需要实现的是:表格头部固定,并且支持水平滚 ...
- 小程序关闭时暂停webview里的音乐
document.addEventListener("visibilitychange", () => { if(document.hidden) { // 页面被 ...
- js算法之寻路
A*寻路算法 算法流程说明: 说明:起始节点记作S,目标节点记作E,对于任意节点P,从S到当前节点P的总移动消耗记作GP,节点P到目标E的曼哈顿距离记作HP,从节点P到相邻节点N的移动消耗记作DPN, ...