#NEWLINE# def get_lookup_constraint( self , constraint_class , alias , targets , sources , lookup_type , raw_value ) : #NEWLINE# #INDENT# from django.db.models.sql.where import SubqueryConstraint , Constraint , AND , OR #NEWLINE# root_constraint = constraint_class ( ) #NEWLINE# assert ( len ( targets ) == len ( sources ) ) #NEWLINE# def get_normalized_value( value ) : #NEWLINE# #INDENT# from django.db.models import Model #NEWLINE# if isinstance ( value , Model ) : #NEWLINE# #INDENT# value_list = [ ] #NEWLINE# for source in sources : #NEWLINE# #INDENT# while ( ( not isinstance ( value , source . model ) ) and source . rel ) : #NEWLINE# #INDENT# source = source . rel . to . _meta . get_field ( source . rel . field_name ) #NEWLINE# #UNINDENT# value_list . append ( getattr ( value , source . attname ) ) #NEWLINE# #UNINDENT# return tuple ( value_list ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( value , tuple ) ) : #NEWLINE# #INDENT# return ( value , ) #NEWLINE# #UNINDENT# #UNINDENT# return value #NEWLINE# #UNINDENT# is_multicolumn = ( len ( self . related_fields ) > 1 ) #NEWLINE# if ( hasattr ( raw_value , '_as_sql' ) or hasattr ( raw_value , 'get_compiler' ) ) : #NEWLINE# #INDENT# root_constraint . add ( SubqueryConstraint ( alias , [ target . column for target in targets ] , [ source . name for source in sources ] , raw_value ) , AND ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( lookup_type == 'isnull' ) : #NEWLINE# #INDENT# root_constraint . add ( ( Constraint ( alias , targets [ 0 ] . column , targets [ 0 ] ) , lookup_type , raw_value ) , AND ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( lookup_type == 'exact' ) or ( ( lookup_type in [ 'gt' , 'lt' , 'gte' , 'lte' ] ) and ( not is_multicolumn ) ) ) : #NEWLINE# #INDENT# value = get_normalized_value ( raw_value ) #NEWLINE# for ( index , source ) in enumerate ( sources ) : #NEWLINE# #INDENT# root_constraint . add ( ( Constraint ( alias , targets [ index ] . column , sources [ index ] ) , lookup_type , value [ index ] ) , AND ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( lookup_type in [ 'range' , 'in' ] ) and ( not is_multicolumn ) ) : #NEWLINE# #INDENT# values = [ get_normalized_value ( value ) for value in raw_value ] #NEWLINE# value = [ val [ 0 ] for val in values ] #NEWLINE# root_constraint . add ( ( Constraint ( alias , targets [ 0 ] . column , sources [ 0 ] ) , root_constraint , value ) , AND ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( lookup_type == 'in' ) : #NEWLINE# #INDENT# values = [ get_normalized_value ( value ) for value in raw_value ] #NEWLINE# for value in values : #NEWLINE# #INDENT# value_constraint = constraint_class ( ) #NEWLINE# for ( index , target ) in enumerate ( targets ) : #NEWLINE# #INDENT# value_constraint . add ( ( Constraint ( alias , target . column , sources [ index ] ) , 'exact' , value [ index ] ) , AND ) #NEWLINE# #UNINDENT# root_constraint . add ( value_constraint , OR ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( 'Related Field got invalid lookup: %s' % lookup_type ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return root_constraint