如何在 openerp(或相同的其他字段)中检索具有相同名称值的记录?
最佳答案
使用手动SQL查询分组的解决方案name
正如 Ruchir 所建议的那样,这可能是最简单的,但有点低级。您也可以使用 read_group()
来做到这一点执行类似 GROUP BY
的 API 方法查询,但不绕过访问控制机制和模型业务逻辑。
read_group
方法采用搜索域(类似于 search()
)、要读取的字段列表(类似于 read()
)和要分组的字段列表。此方法记录在 OpenERP API 中.
它返回一个有序的字典列表,其中包含分组数据和一些额外的值,包括每组中的记录数,存储在名为 <grouped_field>_count
的键中。 ,您可以使用它来查找重复项。
例如,如果您查找重复的 name
值,没有任何其他搜索条件:
def duplicate_names(self, cr, uid, context=None):
# Note: context not propagated for brevity of example
groups = self.read_group(cr, uid, [], ['name'], ['name'])
duplicate_names = [g['name'] for g in groups if g['name_count'] > 1]
print "Duplicates names:", duplicate_names
if duplicate_names:
# It's easy to find out the IDs of duplicate records for each name,
# here is for the first one
duplicate_ids = self.search(cr, uid, [('name', '=', duplicate_names[0])])
print "Duplicate IDs for %r: %s" % (duplicate_names[0], duplicate_ids)
关于python - OpenERP : fetch record with same name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11335945/