旧版本差异参考

当前日期下,所谓的旧版本是指13.0之前的版本。

API差异

one

作用: 表示被封装的方法接受单一的对象,用于处理一些单个对象的方法。 返回值: [None](部分不负责的教程里说不返回值) v7版本写法: def funct(self,cr,uid,ids,context)

举例: 视图文件中添加一个按钮,对应的触发方法:

@api.one
def button_test(self):
    self.name = "xxx"

multi

作用:表示被封装的方法接收一个集合对象,正好与one对应。 返回值: 被封装方法的返回值 v7版本写法: model.method(cr, uid, ids, args, context=context)

multi典型的用法即odoo对象的write方法:

@api.multi
def write(self, vals):
    res = super(Book, self).write(vals)
    return res

这里的self是一个集合对象,vals是待更新的字段和值的字典,这里的例子我们没有用到self,只是调用了父类的write方法,将vals写入到数据库中。如果我们要修改某个值,需要对self进行迭代,然后更新字段的值。

ir.actions

在v12及更早的版本中,还支持以下属性:

  • view_type: 用于展示列表视图的类型,可选值有tree和form。
  • src_model: 该动作绑定的源模型(在该模型上打开动作)。
  • multi:布尔值,True代表仅显示在树形列表视图中。(13.0+版本的替代者是binding_view_types)

web.crash_manager

12.0之前的版本名称为web.crash_manager, 13.0被重新命名为了web.CrashManager.

Request

request的跳转方法在15.0开始作出了调整, 14.0-之前的版本使用方法如下:

@http.route('/book_store/redirect_to_baidu/', auth='public')
def object(self,**kw):
    return werkzeug.utils.redirect("http://www.baidu.com", code=301)

redirect方法接受两个参数,第一个code指明要跳转的URL,相对路径,如果要跳转站外路径需要补全路径。code指明条状状态是301还是302,默认是302跳转。

也可以在request中直接调用redirect方法:

request.redirect("http://www.baidu.com")

name_search方法

name_search方法在14.0时的底层实现发生了变更,我们先来看一下13.0版本的源代码:

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
    """ name_search(name='', args=None, operator='ilike', limit=100) -> records

    Search for records that have a display name matching the given
    ``name`` pattern when compared with the given ``operator``, while also
    matching the optional search domain (``args``).

    This is used for example to provide suggestions based on a partial
    value for a relational field. Sometimes be seen as the inverse
    function of :meth:`~.name_get`, but it is not guaranteed to be.

    This method is equivalent to calling :meth:`~.search` with a search
    domain based on ``display_name`` and then :meth:`~.name_get` on the
    result of the search.

    :param str name: the name pattern to match
    :param list args: optional search domain (see :meth:`~.search` for
                        syntax), specifying further restrictions
    :param str operator: domain operator for matching ``name``, such as
                            ``'like'`` or ``'='``.
    :param int limit: optional max number of records to return
    :rtype: list
    :return: list of pairs ``(id, text_repr)`` for all matching records.
    """
    return self._name_search(name, args, operator, limit=limit)

@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
    # private implementation of name_search, allows passing a dedicated user
    # for the name_get part to solve some access rights issues
    args = list(args or [])
    # optimize out the default criterion of ``ilike ''`` that matches everything
    if not self._rec_name:
        _logger.warning("Cannot execute name_search, no _rec_name defined on %s", self._name)
    elif not (name == '' and operator == 'ilike'):
        args += [(self._rec_name, operator, name)]
    ids = self._search(args, limit=limit, access_rights_uid=name_get_uid)
    recs = self.browse(ids)
    return lazy_name_get(recs.with_user(name_get_uid))

13.0版本的name_search方法底层是由_name_search提供的计算结果,而_name_search则是使用lazy_name_get方法返回的结果,也因此,name_search的查询结果也是一个由id和延迟计算的方法组成的元组。

我们再来看14.0的版本:

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
    """ name_search(name='', args=None, operator='ilike', limit=100) -> records

    Search for records that have a display name matching the given
    ``name`` pattern when compared with the given ``operator``, while also
    matching the optional search domain (``args``).

    This is used for example to provide suggestions based on a partial
    value for a relational field. Sometimes be seen as the inverse
    function of :meth:`~.name_get`, but it is not guaranteed to be.

    This method is equivalent to calling :meth:`~.search` with a search
    domain based on ``display_name`` and then :meth:`~.name_get` on the
    result of the search.

    :param str name: the name pattern to match
    :param list args: optional search domain (see :meth:`~.search` for
                        syntax), specifying further restrictions
    :param str operator: domain operator for matching ``name``, such as
                            ``'like'`` or ``'='``.
    :param int limit: optional max number of records to return
    :rtype: list
    :return: list of pairs ``(id, text_repr)`` for all matching records.
    """
    ids = self._name_search(name, args, operator, limit=limit)
    return self.browse(ids).sudo().name_get()

@api.model
def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
    """ _name_search(name='', args=None, operator='ilike', limit=100, name_get_uid=None) -> ids

    Private implementation of name_search, allows passing a dedicated user
    for the name_get part to solve some access rights issues.
    """
    args = list(args or [])
    # optimize out the default criterion of ``ilike ''`` that matches everything
    if not self._rec_name:
        _logger.warning("Cannot execute name_search, no _rec_name defined on %s", self._name)
    elif not (name == '' and operator == 'ilike'):
        args += [(self._rec_name, operator, name)]
    return self._search(args, limit=limit, access_rights_uid=name_get_uid)

由14.0+的版本我们可以看出,name_search方法的底层实现由lazy_name_get替换为了_search的方法,_name_search方法的返回结果也不再是一个元组列表,而是变成了记录的id列表。

14.0返回结果示例:

---------------
[105811, 105812, 105805, 105809, 105807, 105806, 105798, 105797]

results matching ""

    No results matching ""