def _compute_bank_count(self):
bank_data = self.env['res.partner.bank'].read_group([('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
mapped_data = dict([(bank['partner_id'][0], bank['partner_id_count']) for bank in bank_data])
for partner in self:
partner.bank_account_count = mapped_data.get(partner.id, 0)
如果需要使用search方法,则需要重新组合具有相同字段值的记录
例如,使用位置字段重新组合合作伙伴银行:
from itertools import groupby
keyfunc = lambda r: r.location
partner_banks = sorted(partner_banks, key=keyfunc)
for k, g in groupby(partner_banks, keyfunc):
# k is the location
# list(g) is a list of records with the same location
It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function)
您可以使用read_group方法
您可以在帐户伙伴模型中的_compute_bank_count方法中找到一个示例,该方法通过
partner_id
对银行进行分组:如果需要使用
search
方法,则需要重新组合具有相同字段值的记录例如,使用位置字段重新组合合作伙伴银行:
从itertools.groupby文档中:
相关问题 更多 >
编程相关推荐