#NEWLINE# def build_where_part( self , wheres ) : #NEWLINE# #INDENT# '\n        Recursive method that builds the where parts. Any Q objects that have children will\n        also be built with ``self.build_where_part()``\n\n        :rtype: str\n        :return: The composed where string\n        ' #NEWLINE# where_parts = [ ] #NEWLINE# for where in wheres . children : #NEWLINE# #INDENT# if ( type ( where ) is Q ) : #NEWLINE# #INDENT# where_parts . append ( self . build_where_part ( where ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( where ) is tuple ) : #NEWLINE# #INDENT# field_name = where [ 0 ] #NEWLINE# value = where [ 1 ] #NEWLINE# operator_str = 'eq' #NEWLINE# operator = '=' #NEWLINE# field_parts = field_name . split ( '__' ) #NEWLINE# if ( len ( field_parts ) > 1 ) : #NEWLINE# #INDENT# operator_str = field_parts [ ( - 1 ) ] #NEWLINE# operator = self . get_condition_operator ( operator_str ) #NEWLINE# if ( operator is None ) : #NEWLINE# #INDENT# operator = '=' #NEWLINE# field_name = '.' . join ( joined_parts ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# field_name = '.' . join ( field_parts [ : ( - 1 ) ] ) #NEWLINE# #UNINDENT# trimmed_field_parts = field_name . split ( '.' ) #NEWLINE# if ( len ( trimmed_field_parts ) > 2 ) : #NEWLINE# #INDENT# field_name = '{0}.{1}' . format ( trimmed_field_parts [ 0 ] , '__' . join ( trimmed_field_parts [ 1 : ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( value is None ) : #NEWLINE# #INDENT# operator = 'IS' #NEWLINE# #UNINDENT# condition = '{0} {1} ?' . format ( field_name , operator ) #NEWLINE# if wheres . negated : #NEWLINE# #INDENT# condition = 'NOT({0})' . format ( condition ) #NEWLINE# #UNINDENT# if ( operator_str == 'in' ) : #NEWLINE# #INDENT# if ( type ( value ) is not list ) : #NEWLINE# #INDENT# value = str ( value ) #NEWLINE# value = value . split ( ',' ) #NEWLINE# #UNINDENT# if ( len ( value ) == 0 ) : #NEWLINE# #INDENT# value = [ None ] #NEWLINE# #UNINDENT# if ( type ( value ) is Expression ) : #NEWLINE# #INDENT# condition = condition . replace ( '?' , value . str ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# named_args = [ ] #NEWLINE# for value_item in value : #NEWLINE# #INDENT# named_arg = self . set_arg ( value_item ) #NEWLINE# named_args . append ( '%({0})s' . format ( named_arg ) ) #NEWLINE# #UNINDENT# condition = condition . replace ( '?' , '({0})' . format ( ',' . join ( named_args ) ) , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# value = self . get_condition_value ( operator_str , value ) #NEWLINE# if ( type ( value ) is Expression ) : #NEWLINE# #INDENT# condition = condition . replace ( '?' , value . str ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# named_arg = self . set_arg ( value ) #NEWLINE# condition = condition . replace ( '?' , '%({0})s' . format ( named_arg ) , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# where_parts . append ( condition ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# joined_parts = ' {0} ' . format ( wheres . connector ) . join ( where_parts ) #NEWLINE# return '({0})' . format ( joined_parts )