django-closuretree
django-closuretree copied to clipboard
If I just wanna choose the 5th depth descendant,not the 5 depth,How to change code.thanks a lot!
If I just wanna choose the 5th depth descendant,not the 5 depth, How to change code.thanks a lot!
Hmm, this isn't something that's exposed right now.
If you look at this chunk from the source:
def get_descendants(self, include_self=False, depth=None):
"""Return all the descendants of this object."""
params = {"%s__parent" % self._closure_childref():self.pk}
if depth is not None:
params["%s__depth__lte" % self._closure_childref()] = depth
descendants = self._toplevel().objects.filter(**params)
if not include_self:
descendants = descendants.exclude(pk=self.pk)
return descendants.order_by("%s__depth" % self._closure_childref())
you can do something similar, I think.
Untested, written in Github UI:
params = {"%s__parent" % self._closure_childref():self.pk}
params["%s__depth" % self._closure_childref()] = depth
descendants = yourmodel._toplevel().objects.filter(**params)
descendants.order_by("%s__depth" % self._closure_childref())
Note the changing of the depth__lte parameter to depth, so it's exact instead of less than.