#NEWLINE# def items_json( request ) : #NEWLINE# #INDENT# inventory_items = InventoryItem . objects . values ( 'name' , 'version' ) . annotate ( num_machines = Count ( 'machine' ) ) #NEWLINE# inventory_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for item in inventory_items : #NEWLINE# #INDENT# name = item [ 'name' ] #NEWLINE# version = item [ 'version' ] #NEWLINE# machine_count = item [ 'num_machines' ] #NEWLINE# if ( not ( name in inventory_dict ) ) : #NEWLINE# #INDENT# inventory_dict [ name ] = [ ] #NEWLINE# #UNINDENT# inventory_dict [ name ] . append ( { #NEWLINE# #INDENT# 'version' : version , #NEWLINE# 'count' : machine_count , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# rows = [ ] #NEWLINE# for ( name , versions ) in inventory_dict . items ( ) : #NEWLINE# #INDENT# rows . append ( { #NEWLINE# #INDENT# 'name' : name , #NEWLINE# 'versions' : versions , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# return HttpResponse ( json . dumps ( rows ) , mimetype = 'application/json' )