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修饰符的更多相关文章

  1. 【转】odoo 新API装饰器中one、model、multi的区别

    http://blog.csdn.net/qq_18863573/article/details/51114893 1.one装饰器详解 odoo新API中定义方式: date=fields.Date ...

  2. Odoo 新 API 概述

    __all__ = [ 'Environment', 'Meta', 'guess', 'noguess', 'model', 'multi', 'one', 'cr', 'cr_context', ...

  3. Odoo 二次开发教程(五)-新API的介绍与应用

    [关于odoo新API的介绍,Internet上资料很少,或者不够完整详实,这会对初学者造成很大的困惑,本篇的目的就是希望能帮助新手了解新API的大概] odoo 新api的实现是借助于python装 ...

  4. Java开发笔记(五十)几种开放性修饰符

    前面介绍子类继承父类的时候,提到了public(公共)和private(私有)两个修饰符,其中public表示它所修饰的实体是允许外部访问的:而private表示它所修饰的实体不允许外部访问,只能在当 ...

  5. .NET4.5新特性async和await修饰符实现异步编程

    开篇 每一个版本的.net都会引入一些新的特性,这些特性方便开发人员能够快速实现一些功能.虽然.net版本一直在更新,但是新版本对旧版本的程序都是兼容的,在这一点上微软做的还是非常好的.每次学一个新内 ...

  6. php新版本号废弃 preg_replace /e 修饰符

    近期serverphp版本号升级到了 5.6  发现出了非常多警告 preg_replace(): The /e modifier is deprecated, use preg_replace_ca ...

  7. JavaSE(三)之static、final、abstract修饰符

    一.static修饰符 1.1.static变量 在类中,使用static修饰的成员变量,就是静态变量,反之为非静态变量. 静态变量和非静态变量的区别            静态变量属于类的,&quo ...

  8. Java基础知识回顾之二 ----- 修饰符和String

    前言 在上一篇中,回顾了Java的基本数据类型 ,这篇就来回顾下Java中的一些修饰符以及String. 修饰符介绍 Java修饰符主要分为两类: 访问修饰符 非访问修饰符 其中访问修饰符主要包括 p ...

  9. C语言常用修饰符

    前言 这两天在梳理自己C语言的知识,发现写了这么久的代码,居然所有的知识点都在自己的脑袋里.这可不好,万一老了呢.... 接下来的几天里,会以文字的形式,将这些知识整理出来,分享给大家. 想要看看英文 ...

随机推荐

  1. Python自学:第五章 使用函数range( )

    # -*- coding: GBK -*- for value in range(1,5): print(value) 输出为: 1 2 3 4

  2. java8--Stream的flatmap与map异同的理解

    大纲: 异同点 示例 一.异同点 他们的相同点是接收的入参都是一个function. 不同点这个入参function的返回不同.map返回一个对象,flatmap返回一个stream. 这就使得map ...

  3. .Net 动态编译(c# 脚本)

    1 用.NET提供的类动态编译代码字符串,生成DLL存于内存中,加载到程序域 2 用反射的方式调用这个DLL 将要被编译和执行的代码读入并以字符串方式保存声明CSharpCodeProvider对象实 ...

  4. identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xxKey@74e8f4a3; nested exception is org.hibernate.HibernateException: identifier of an instance of xxentity was altered from错误

    用entityManager保存数据时报错如下 identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xx ...

  5. VC中隐藏和显示IDC_STATIC

    void CImageShowAndHideDlg::OnBnClickedButton1() //隐藏 {       CWnd* pWnd = GetDlgItem(IDC_STATIC);    ...

  6. Responder对象

    Responder对象 响应者是一个对象,它可以响应事件并处理它们.所有响应者对象是类的,最终从UIResponder的( IOS)或NSResponder ( OS X)继承实例.这些类声明一个编程 ...

  7. 解决div设置浮动,高度消失

    给包围 浮动的层 加清除浮动样式,样式要兼容的用下面的代码.clearfix {*zoom:1; clear:both;}.clearfix:after{content:".";d ...

  8. git -- 项目开发最常用操作记录

    官方Git - Book https://git-scm.com/book/zh/v2 ------------------------------git配置以及公钥生成--------------- ...

  9. USACO 2008 November Gold Cheering up the Cows /// MST oj24381

    题目大意: 输入n,p:n个点,p条路 接下来n行输入c[]:在各个点需要花费的时间 接下来p行输入u,v,w:u点到v点的路需要花费时间w 求经过所有点且最后回到起点的最少花费时间 https:// ...

  10. 基于windows消息的响应USB插入或取出

    导语:当有设备进入windows时,系统会向所有的应用层发送WM_DEVICECHANGE消息.进一步根据相应的事件判断设备. LRESULT CALLBACK WndProc(HWND hWnd, ...