Complex Django filter question

Posted by HWM-Rocker on Stack Overflow See other posts from Stack Overflow or by HWM-Rocker
Published on 2010-04-15T20:58:09Z Indexed on 2010/04/15 21:23 UTC
Read the original article Hit count: 326

Filed under:
|

Lets say I have this class (simplified):

class Tag (...):
    children = models.ManyToManyField(null=True, symmetrical=False)

Now I already implemented the functions get_parents, get_all_ancestors. Is there a nice pythonic way to just the top level tags? If I had designed my Tags differently (to point to the parents instead) I would just make get_all_parents().filter(children=null).

My first thought is to create a new function that will go recursively through all parents and save those that has none.

But is there a possibility with filters or Query-objects to do the same (with fewer lines of code)?

Thanks for your help.

[edit]

When it is finished, it should be a hierarchical tagging system. Each tag can have children, parents, but only the children are saved. I want to get all the top level tags, that point through many children / childrens children to my tag.

© Stack Overflow or respective owner

Related posts about django-models

Related posts about django