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')

转载于:https://www.cnblogs.com/dancesir/p/7025972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值