#NEWLINE# def _build_tree_structure( queryset ) : #NEWLINE# #INDENT# '\n    Build an in-memory representation of the item tree, trying to keep\n    database accesses down to a minimum. The returned dictionary looks like\n    this (as json dump):\n\n        {"6": [7, 8, 10]\n         "7": [12],\n         "8": [],\n         ...\n         }\n    ' #NEWLINE# all_nodes = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# mptt_opts = queryset . model . _mptt_meta #NEWLINE# items = queryset . order_by ( mptt_opts . tree_id_attr , mptt_opts . left_attr ) #NEWLINE# values_list = items . values_list ( 'pk' , ( '%s_id' % mptt_opts . parent_attr ) ) #NEWLINE# for ( p_id , parent_id ) in values_list : #NEWLINE# #INDENT# all_nodes [ p_id ] = [ ] #NEWLINE# if parent_id : #NEWLINE# #INDENT# if ( parent_id not in all_nodes ) : #NEWLINE# #INDENT# all_nodes [ parent_id ] = [ ] #NEWLINE# logger . warn ( "Incorrect MPTT hierarchy for %s, node %d has left_attr < than one of its parents. Try rebuilding mptt data (use '%s._default_manager.rebuild()')." , queryset . model . __name__ , p_id , queryset . model . __name__ ) #NEWLINE# #UNINDENT# all_nodes [ parent_id ] . append ( p_id ) #NEWLINE# #UNINDENT# #UNINDENT# return all_nodes