(self, node, ascending=False, include_self=False)
return (
            cte.queryset()
            .with_cte(cte)
            .order_by(("" if ascending else "-") + "_depth")
        )
Query node ancestors

        :param node: A model instance or a QuerySet or Q object querying
        the adjacency list model. If a QuerySet, it should query a
        single value with something like `.values('id')`. If Q the
        `include_self` argument will be ignored.
        :param ascending: Order of results. The default (`False`) gets
        results in descending order (root ancestor first, immediate
        parent last).
        :param include_self:
        :returns: A `QuerySet` instance.
Query node ancestors
Query node ancestors
def get_ancestors(self, node, ascending=False, include_self=False):
        """Query node ancestors

        :param node: A model instance or a QuerySet or Q object querying
        the adjacency list model. If a QuerySet, it should query a
        single value with something like `.values('id')`. If Q the
        `include_self` argument will be ignored.
        :param ascending: Order of results. The default (`False`) gets
        results in descending order (root ancestor first, immediate
        parent last).
        :param include_self:
        :returns: A `QuerySet` instance.
        """
        if isinstance(node, Q):
            where = node
        elif include_self:
            if isinstance(node, QuerySet):
                if _is_empty(node):
                    return self.none()
                where = Q(id__in=node.order_by())
            else:
                where = Q(id=node.id)
        elif isinstance(node, QuerySet):
            if _is_empty(node):
                return self.none()
            where = Q(id__in=node.order_by().values("parent_id"))
        else:
            where = Q(id=node.parent_id)

        def make_cte_query(cte):
            return self.filter(where).order_by().annotate(
                _depth=Value(0, output_field=field),
            ).union(
                cte.join(
                    self.all().order_by(),
                    id=cte.col.parent_id,
                ).annotate(
                    _depth=cte.col._depth + Value(1, output_field=field),
                ),
            )

        cte = With.recursive(make_cte_query)
        return (
            cte.queryset()
            .with_cte(cte)
            .order_by(("" if ascending else "-") + "_depth")
        )
def get_ancestors ( self , node , ascending = False , include_self = False ) : if isinstance ( node , Q ) : where = node elif include_self : if isinstance ( node , QuerySet ) : if _is_empty ( node ) : return self . none ( ) where = Q ( id__in = node . order_by ( ) ) else : where = Q ( id = node . id ) elif isinstance ( node , QuerySet ) : if _is_empty ( node ) : return self . none ( ) where = Q ( id__in = node . order_by ( ) . values ( "parent_id" ) ) else : where = Q ( id = node . parent_id ) def make_cte_query ( cte ) : return self . filter ( where ) . order_by ( ) . annotate ( _depth = Value ( 0 , output_field = field ) , ) . union ( cte . join ( self . all ( ) . order_by ( ) , id = cte . col . parent_id , ) . annotate ( _depth = cte . col . _depth + Value ( 1 , output_field = field ) , ) , ) cte = With . recursive ( make_cte_query ) return ( cte . queryset ( ) . with_cte ( cte ) . order_by ( ( "" if ascending else "-" ) + "_depth" ) )