我需要从子对象中获取所有父对象。我不知道它会有多少个父对象,所以我想我需要在这里使用递归。
所以我有一个对象,它可能有或没有其他继承的对象。如果它有任何继承的对象,我需要将该对象id添加到列表中,然后检查继承的对象(或对象)是否有父对象,如果有,则将这些父对象id添加至同一列表中并检查其父对象,依此类推。当最深的对象没有任何父对象时,应该停止。
所以我的代码现在看起来像这样:
def get_parent_groups(self, cr, uid, ids, grp_id, context=None):
res = [] # list for parent ids
grp_obj = self.pool.get('res.groups')
#grp_id is childs `id` it means same thing as
# `parent_grp.id`, only that latter is parent object id.
grp = grp_obj.browse(cr, uid, grp_id)
if grp.implied_ids: #check if child has any parent ids
for parent_grp in grp.implied_ids:
res.append(parent_grp.id) #append found parent ids
return res
此代码仅获取第一个父对象ID。所以从这里开始,我想我应该放递归,让所有其他的父母都来,但我无法理解我应该如何正确地得到它。有什么帮助吗?
附笔。
当我看到有人问我什么样
cr, uid
意思是,我没有指定它,因为我认为它与这个问题无关(因为它确实不相关),但为了明确说明,OpenERp框架方法需要这些输入:
`cr` - database cursor
`uid` - current user id
`ids` - ids of the object
但正如我所说,这些和问题无关,我只是发布了工作方法,以避免遗漏任何内容。