#NEWLINE# def test_more_more_more( self ) : #NEWLINE# #INDENT# books = Book . objects . all ( ) #NEWLINE# books . aggregate ( Avg ( 'authors__age' ) ) #NEWLINE# self . assertQuerysetEqual ( books . all ( ) , [ 'Artificial Intelligence: A Modern Approach' , 'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp' , 'Practical Django Projects' , 'Python Web Development with Django' , 'Sams Teach Yourself Django in 24 Hours' , 'The Definitive Guide to Django: Web Development Done Right' ] , ( lambda b : b . name ) ) #NEWLINE# qs = Book . objects . annotate ( num_authors = Count ( 'authors' ) ) . filter ( num_authors = 2 ) . dates ( 'pubdate' , 'day' ) #NEWLINE# self . assertQuerysetEqual ( qs , [ datetime . datetime ( 1995 , 1 , 15 , 0 , 0 ) , datetime . datetime ( 2007 , 12 , 6 , 0 , 0 ) ] , ( lambda b : b ) ) #NEWLINE# qs = Book . objects . annotate ( mean_auth_age = Avg ( 'authors__age' ) ) . extra ( select = { #NEWLINE# #INDENT# 'sheets' : '(pages + %s) / %s' , #NEWLINE# #UNINDENT# } , select_params = [ 1 , 2 ] ) . order_by ( 'sheets' ) . values ( 'sheets' ) #NEWLINE# self . assertQuerysetEqual ( qs , [ 150 , 175 , 224 , 264 , 473 , 566 ] , ( lambda b : int ( b [ 'sheets' ] ) ) ) #NEWLINE# self . assertEqual ( Book . objects . values ( 'publisher' ) . annotate ( Count ( 'publisher' ) ) . count ( ) , 4 ) #NEWLINE# self . assertEqual ( Book . objects . annotate ( Count ( 'publisher' ) ) . values ( 'publisher' ) . count ( ) , 6 ) #NEWLINE# publishers = Publisher . objects . filter ( id__in = [ 1 , 2 ] ) #NEWLINE# self . assertQuerysetEqual ( publishers , [ 'Apress' , 'Sams' ] , ( lambda p : p . name ) ) #NEWLINE# publishers = publishers . annotate ( n_books = Count ( 'book' ) ) #NEWLINE# self . assertEqual ( publishers [ 0 ] . n_books , 2 ) #NEWLINE# self . assertQuerysetEqual ( publishers , [ 'Apress' , 'Sams' ] , ( lambda p : p . name ) ) #NEWLINE# books = Book . objects . filter ( publisher__in = publishers ) #NEWLINE# self . assertQuerysetEqual ( books , [ 'Practical Django Projects' , 'Sams Teach Yourself Django in 24 Hours' , 'The Definitive Guide to Django: Web Development Done Right' ] , ( lambda b : b . name ) ) #NEWLINE# self . assertQuerysetEqual ( publishers , [ 'Apress' , 'Sams' ] , ( lambda p : p . name ) ) #NEWLINE# self . assertEqual ( HardbackBook . objects . aggregate ( n_pages = Sum ( 'book_ptr__pages' ) ) , { #NEWLINE# #INDENT# 'n_pages' : 2078 , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assertEqual ( HardbackBook . objects . aggregate ( n_pages = Sum ( 'pages' ) ) , { #NEWLINE# #INDENT# 'n_pages' : 2078 , #NEWLINE# #UNINDENT# } ) #NEWLINE# qs = HardbackBook . objects . annotate ( n_authors = Count ( 'book_ptr__authors' ) ) . values ( 'name' , 'n_authors' ) #NEWLINE# self . assertQuerysetEqual ( books , [ { #NEWLINE# #INDENT# 'n_authors' : 2 , #NEWLINE# 'name' : 'Artificial Intelligence: A Modern Approach' , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'n_authors' : 1 , #NEWLINE# 'name' : 'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp' , #NEWLINE# #UNINDENT# } ] , ( lambda h : h ) ) #NEWLINE# qs = HardbackBook . objects . annotate ( n_authors = Count ( 'authors' ) ) . values ( 'name' , 'n_authors' ) #NEWLINE# self . assertQuerysetEqual ( qs , [ { #NEWLINE# #INDENT# 'n_authors' : 2 , #NEWLINE# 'name' : 'Artificial Intelligence: A Modern Approach' , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'n_authors' : 1 , #NEWLINE# 'name' : 'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp' , #NEWLINE# #UNINDENT# } ] , ( lambda h : h ) ) #NEWLINE# self . assertRaises ( FieldError , ( lambda : Book . objects . annotate ( mean_age = Avg ( 'authors__age' ) ) . annotate ( Avg ( 'mean_age' ) ) ) )