def cache_tree_children(queryset):
    """
    For all items in the queryset, set the '_cached_children' attribute to a
    list. This attribute is in turn used by the 'get_children' method on the
    item, which would otherwise (if '_cached_children' is not set) cause a 
    database query.

    The queryset must be ordered by 'lft', or the function will put the children
    in the wrong order.
    """
    parents_dict = {}
    for obj in queryset:
        parents_dict[obj.pk] = obj
        obj._cached_children = []
    for obj in queryset:
        parent = parents_dict.get(obj.parent_id)
        if parent:
            parent._cached_children.append(obj)
