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')
posted @ 2017-06-16 09:45  553490191  阅读(202)  评论(0编辑  收藏  举报