zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    python文件处理-读、写
    Python三元运算和lambda表达式
    可变、不可变数据类型和hash
    Python常见字符编码间的转换
    Python常用字符编码
    spring cloud 使用spring cloud bus自动刷新配置
    spring cloud config--client
    spring cloud Config--server
    git 小乌龟安装教程
    spring cloud Eureka常见问题总结
  • 原文地址:https://www.cnblogs.com/dancesir/p/6931866.html
Copyright © 2011-2022 走看看