-1

I need to get all parent objects from child object. I don't know how many parent objects it will have, so I think I would need to use recursion here somehow.

So I have an object, that may have or not have other inherited objects. If it has any inherited object, I need to add that objects id to list and then check if that inherited object (or objects) have parents, then if it does, then add those parents ids into same list and check their parents and so on. It should stop when deepest object does not have any parents.

So my code now looks like this:

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

This code only get first parent object ids. So from here I think I should put recursion and get all other parents, but I can't wrap my head how I should properly get it. Any help?

P.S. As I see some one asked what things like cr, uid mean, I didn't specify it as I thought it is not related with this problem (as it is really not), but to make it clear these inputs are needed in OpenERp framework methods:

`cr` - database cursor 
`uid` - current user id
`ids` - ids of the object

But as I said, these are not related with problem, I just posted working method to not miss anything.

Bach
  • 6,145
  • 7
  • 36
  • 61
Andrius
  • 19,658
  • 37
  • 143
  • 243
  • possible duplicate of [get python class parent(s)](http://stackoverflow.com/questions/2611892/get-python-class-parents) – Matthew Trevor Mar 31 '14 at 06:01
  • Not sure if this is what you need but: `inspect.getmro(cls)` returns a tuple of class cls’s base classes, including cls, in method resolution order. Documentation: https://docs.python.org/2/library/inspect.html – skamsie Mar 31 '14 at 06:02
  • Why down vote question? – Andrius Mar 31 '14 at 07:48

1 Answers1

0

I solved this problem rewriting method like this. Don't know if such way is the best way to go, but it is working:

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
Andrius
  • 19,658
  • 37
  • 143
  • 243
  • Since you answered your own question can you also make a demo explaining what are all the params and how your return looks like? – skamsie Mar 31 '14 at 06:14