def _build_tree_structure(queryset):
    """
    Build an in-memory representation of the item tree, trying to keep
    database accesses down to a minimum. The returned dictionary looks like
    this (as json dump):

        {"6": [7, 8, 10]
         "7": [12],
         "8": [],
         ...
         }
    """
    all_nodes = {}

    mptt_opts = queryset.model._mptt_meta
    items = queryset.order_by(mptt_opts.tree_id_attr, mptt_opts.left_attr).values_list(
        "pk", "%s_id" % mptt_opts.parent_attr
    )
    for p_id, parent_id in items:
        all_nodes.setdefault(str(parent_id) if parent_id else 0, []).append(p_id)
    return all_nodes

    Build an in-memory representation of the item tree, trying to keep
    database accesses down to a minimum. The returned dictionary looks like
    this (as json dump):

        {"6": [7, 8, 10]
         "7": [12],
         "8": [],
         ...
         }
    
feincms/admin/tree_editor.py