django-closuretree icon indicating copy to clipboard operation
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!

Open showercoding opened this issue 7 years ago • 1 comments

If I just wanna choose the 5th depth descendant,not the 5 depth, How to change code.thanks a lot!

showercoding avatar Jan 01 '18 11:01 showercoding

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.

mikebryant avatar Jan 02 '18 11:01 mikebryant