#NEWLINE# def test_defer( self ) : #NEWLINE# #INDENT# s1 = Secondary . objects . create ( first = 'x1' , second = 'y1' ) #NEWLINE# p1 = Primary . objects . create ( name = 'p1' , value = 'xx' , related = s1 ) #NEWLINE# qs = Primary . objects . all ( ) #NEWLINE# self . assert_delayed ( qs . defer ( 'name' ) [ 0 ] , 1 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' ) [ 0 ] , 2 ) #NEWLINE# self . assert_delayed ( qs . defer ( 'related__first' ) [ 0 ] , 0 ) #NEWLINE# self . assert_delayed ( qs . only ( 'pk' ) [ 0 ] , 3 ) #NEWLINE# obj = qs . select_related ( ) . only ( 'related__first' ) [ 0 ] #NEWLINE# self . assert_delayed ( obj , 2 ) #NEWLINE# self . assertEqual ( obj . related_id , s1 . pk ) #NEWLINE# self . assert_delayed ( s1 . primary_set . all ( ) . only ( 'pk' ) [ 0 ] , 3 ) #NEWLINE# self . assert_delayed ( qs . defer ( 'name' ) . extra ( select = { #NEWLINE# #INDENT# 'a' : 1 , #NEWLINE# #UNINDENT# } ) [ 0 ] , 1 ) #NEWLINE# self . assert_delayed ( qs . extra ( select = { #NEWLINE# #INDENT# 'a' : 1 , #NEWLINE# #UNINDENT# } ) . defer ( 'name' ) [ 0 ] , 1 ) #NEWLINE# self . assert_delayed ( qs . defer ( 'name' ) . defer ( 'value' ) [ 0 ] , 2 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' ) . only ( 'value' ) [ 0 ] , 2 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' ) . defer ( 'value' ) [ 0 ] , 2 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' , 'value' ) . defer ( 'value' ) [ 0 ] , 2 ) #NEWLINE# self . assert_delayed ( qs . defer ( 'name' ) . only ( 'value' ) [ 0 ] , 2 ) #NEWLINE# obj = qs . only ( ) [ 0 ] #NEWLINE# self . assert_delayed ( qs . defer ( None ) [ 0 ] , 0 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' ) . defer ( None ) [ 0 ] , 0 ) #NEWLINE# self . assertEqual ( qs . defer ( 'name' ) . values ( ) [ 0 ] , { #NEWLINE# #INDENT# 'id' : p1 . id , #NEWLINE# 'name' : 'p1' , #NEWLINE# 'value' : 'xx' , #NEWLINE# 'related_id' : s1 . id , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assertEqual ( qs . only ( 'name' ) . values ( ) [ 0 ] , { #NEWLINE# #INDENT# 'id' : p1 . id , #NEWLINE# 'name' : 'p1' , #NEWLINE# 'value' : 'xx' , #NEWLINE# 'related_id' : s1 . id , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assert_delayed ( qs . defer ( 'name' ) . get ( pk = p1 . pk ) , 1 ) #NEWLINE# self . assert_delayed ( qs . only ( 'name' ) . get ( pk = p1 . pk ) , 2 ) #NEWLINE# with self . assertRaises ( InvalidQuery ) : #NEWLINE# #INDENT# qs . only ( 'name' ) . select_related ( 'related' ) [ 0 ] #NEWLINE# #UNINDENT# with self . assertRaises ( InvalidQuery ) : #NEWLINE# #INDENT# qs . defer ( 'related' ) . select_related ( 'related' ) [ 0 ] #NEWLINE# #UNINDENT# with self . assertNumQueries ( 3 ) : #NEWLINE# #INDENT# obj = qs . defer ( 'related' ) . select_related ( ) [ 0 ] #NEWLINE# self . assert_delayed ( obj , 1 ) #NEWLINE# self . assertEqual ( obj . related . id , s1 . pk ) #NEWLINE# #UNINDENT# obj = Primary . objects . defer ( 'value' ) . get ( name = 'p1' ) #NEWLINE# obj . name = 'a new name' #NEWLINE# obj . save ( ) #NEWLINE# self . assertQuerysetEqual ( Primary . objects . all ( ) , [ 'a new name' ] , ( lambda p : p . name ) ) #NEWLINE# Child . objects . create ( name = 'c1' , value = 'foo' , related = s1 ) #NEWLINE# obj = Child . objects . defer ( 'value' ) . get ( name = 'c1' ) #NEWLINE# self . assert_delayed ( obj , 1 ) #NEWLINE# self . assertEqual ( obj . name , 'c1' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# obj . name = 'c2' #NEWLINE# obj . save ( ) #NEWLINE# obj = Child . objects . only ( 'name' ) . get ( name = 'c2' ) #NEWLINE# self . assert_delayed ( obj , 3 ) #NEWLINE# self . assertEqual ( obj . name , 'c2' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# obj . name = 'cc' #NEWLINE# obj . save ( ) #NEWLINE# BigChild . objects . create ( name = 'b1' , value = 'foo' , related = s1 , other = 'bar' ) #NEWLINE# obj = BigChild . objects . defer ( 'value' ) . get ( name = 'b1' ) #NEWLINE# self . assert_delayed ( self , 1 ) #NEWLINE# self . assertEqual ( obj . name , 'b1' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# self . assertEqual ( obj . other , 'bar' ) #NEWLINE# obj . name = 'b2' #NEWLINE# obj . save ( ) #NEWLINE# obj = BigChild . objects . defer ( 'other' ) . get ( name = 'b2' ) #NEWLINE# self . assert_delayed ( obj , 1 ) #NEWLINE# self . assertEqual ( obj . name , 'b2' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# self . assertEqual ( obj . other , 'bar' ) #NEWLINE# obj . name = 'b3' #NEWLINE# obj . save ( ) #NEWLINE# obj = BigChild . objects . only ( 'name' ) . get ( name = 'b3' ) #NEWLINE# self . assert_delayed ( obj , 4 ) #NEWLINE# self . assertEqual ( obj . name , 'b3' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# self . assertEqual ( obj . other , 'bar' ) #NEWLINE# obj . name = 'b4' #NEWLINE# obj . save ( ) #NEWLINE# obj = BigChild . objects . only ( 'other' ) . get ( name = 'b4' ) #NEWLINE# self . assert_delayed ( obj , 4 ) #NEWLINE# self . assertEqual ( obj . name , 'b4' ) #NEWLINE# self . assertEqual ( obj . value , 'foo' ) #NEWLINE# self . assertEqual ( obj . other , 'bar' ) #NEWLINE# obj . name = 'bb' #NEWLINE# obj . save ( )