如何在使用 "read" 方法调用 python 对象时解决此 "RuntimeError"?:<< 超出最大递归深度 >>

How do I solve this "RuntimeError" while calling a python object with the "read" method?: << Maximum recursion depth exceeded >>

我正在研究 Odoo。我正在尝试使用 read() 方法读取字段值。

这是我的代码:

def name_get(self, cr, uid, ids, context=None):
    if isinstance(ids, (list, tuple)) and not len(ids):
        return []
    if isinstance(ids, (long, int)):
        ids = [ids]
    reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
    res = []
    for record in reads:
        name = record['name']
        if record['parent_id']:
            name = record['parent_id'][1]+' / '+name
        res.append((record['id'], name))
    return res

发生错误:

RuntimeError: maximum recursion depth exceeded

我该如何解决?

不要使用read方法,而是使用browsesearch方法。检查这是否适合您:

def name_get(self, cr, uid, ids, context=None):

    # [...]

    res=[]
    for record in self.browse(cr, uid, ids, context=context):
        name = record.name
        if record.parent_id:
            name = record.parent_id[0] + ' / ' + name
        res.append((record.id, name))
    return res