zoukankan      html  css  js  c++  java
  • Odoo Documentation : Recordsets

    Other recordset operations

    Recordsets are iterable(可迭代的) so the usual Python tools are available for transformation (map()sorted()ifilter(), ...) however these return either a list or an iterator, removing the ability(能力) to call methods on their result, or to use set operations.

    Recordsets therefore provide these operations returning recordsets themselves (when possible):

    filtered()

    returns a recordset containing only records satisfying the provided predicate function(判定函数). The predicate can also be a string to filter by a field being true or false:

    # only keep records whose company is the current user's
    records.filtered(lambda r: r.company_id == user.company_id)
    
    # only keep records whose partner is a company
    records.filtered("partner_id.is_company")
    sorted()

    returns a recordset sorted by the provided key function. If no key is provided, use the model's default sort order:

    # sort records by name
    records.sorted(key=lambda r: r.name)
    mapped()

    applies the provided function to each record in the recordset, returns a recordset if the results are recordsets:

    # returns a list of summing two fields for each record in the set
    records.mapped(lambda r: r.field1 + r.field2)

    The provided function can be a string to get field values:

    # returns a list of names
    records.mapped('name')
    
    # returns a recordset of partners
    record.mapped('partner_id')
    
    # returns the union of all partner banks, with duplicates removed
    record.mapped('partner_id.bank_ids')
  • 相关阅读:
    文件权限命令
    复制、移动文件及目录命令
    创建、删除文件及目录命令
    绝对路径和相对路径
    查找文件命令
    链接命令
    文本搜索命令
    编辑器 vim
    有参装饰器与迭代器
    闭包函数与装饰器
  • 原文地址:https://www.cnblogs.com/dancesir/p/7025972.html
Copyright © 2011-2022 走看看