【发布时间】:2014-03-31 05:43:16
【问题描述】:
我需要从子对象中获取所有父对象。我不知道它会有多少父对象,所以我想我需要以某种方式在这里使用递归。
所以我有一个对象,它可能有也可能没有其他继承的对象。如果它有任何继承对象,我需要将该对象 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
但正如我所说,这些与问题无关,我只是发布了工作方法,以免错过任何内容。
【问题讨论】:
-
get python class parent(s) 的可能重复项
-
不确定这是否是您需要的,但:
inspect.getmro(cls)按方法解析顺序返回类 cls 的基类(包括 cls)的元组。文档:docs.python.org/2/library/inspect.html -
为什么要投反对票?