【问题标题】:Get all parent 'objects'?获取所有父“对象”?
【发布时间】: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

但正如我所说,这些与问题无关,我只是发布了工作方法,以免错过任何内容。

【问题讨论】:

标签: python recursion openerp


【解决方案1】:

我用这样的重写方法解决了这个问题。不知道这种方式是否是最好的方式,但它正在工作:

def get_parent_groups(self, cr, uid, ids, grp_id, context=None):
    res = []     
    grp_obj = self.pool.get('res.groups')
    grp = grp_obj.browse(cr, uid, grp_id)
    if grp.implied_ids:
        for parent_grp in grp.implied_ids:
            res.append(parent_grp.id)            
            if parent_grp.implied_ids:
                parent_ids = self.get_parent_groups(cr, uid, ids, parent_grp.id)
                for parent_id in parent_ids:
                    res.append(parent_id)
    return res

【讨论】:

  • 既然您回答了自己的问题,您是否还可以制作一个演示来解释所有参数是什么以及您的回报是什么样的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多