How to get all objects with their children using django orm?

Posted by kender on Stack Overflow See other posts from Stack Overflow or by kender
Published on 2010-05-05T07:30:19Z Indexed on 2010/05/05 10:48 UTC
Read the original article Hit count: 204

Filed under:
|
|

Hi, I got very simple hierarchical structure: every object can have 0 or 1 parent. There's no limit on how many children each object can have.

So in my application I got such a model:

class O(Model):
  name = CharField(max_length = 20)
  parent = ForeignKey('O', related_name = 'children')

Now I would like to be able to fetch all objects who have a particular one Object1 in their parent-tree (as in their parent or parent of their parents, etc).

Should I use mptt or is there a simpler approach?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models