#NEWLINE# def _check_relationship_model( self , from_model = None , **kwargs ) : #NEWLINE# #INDENT# if hasattr ( self . remote_field . through , '_meta' ) : #NEWLINE# #INDENT# qualified_model_name = ( '%s.%s' % ( self . remote_field . through . _meta . app_label , self . remote_field . through . __name__ ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# qualified_model_name = self . remote_field . through #NEWLINE# #UNINDENT# errors = [ ] #NEWLINE# if ( self . remote_field . through not in self . opts . apps . get_models ( include_auto_created = True ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "Field specifies a many-to-many relation through model '%s', which has not been installed." % qualified_model_name ) , obj = self , id = 'fields.E331' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert ( from_model is not None ) , "ManyToManyField with intermediate tables cannot be checked if you don't pass the model where the field is attached to." #NEWLINE# to_model = resolve_relation ( from_model , self . remote_field . model ) #NEWLINE# from_model_name = from_model . _meta . object_name #NEWLINE# if isinstance ( to_model , six . string_types ) : #NEWLINE# #INDENT# to_model_name = to_model #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# to_model_name = to_model . _meta . object_name #NEWLINE# #UNINDENT# relationship_model_name = self . remote_field . through . _meta . object_name #NEWLINE# self_referential = ( from_model == to_model ) #NEWLINE# if ( self_referential and self . remote_field . symmetrical and ( not self . remote_field . through . _meta . auto_created ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( 'Many-to-many fields with intermediate tables must not be symmetrical.' , obj = self , id = 'fields.E332' ) ) #NEWLINE# #UNINDENT# if self_referential : #NEWLINE# #INDENT# seen_self = sum ( ( ( from_model == getattr ( field . remote_field , 'model' , None ) ) for field in self . remote_field . through . _meta . fields ) ) #NEWLINE# if ( ( seen_self > 2 ) and ( not self . remote_field . through_fields ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "The model is used as an intermediate model by '%s', but it has more than two foreign keys to '%s', which is ambiguous. You must specify which two foreign keys Django should use via the through_fields keyword argument." % ( self , from_model_name ) ) , hint = 'Use through_fields to specify which two foreign keys Django should use.' , obj = self . remote_field . through , id = 'fields.E333' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# seen_from = sum ( ( ( from_model == getattr ( field . remote_field , 'model' , None ) ) for field in self . remote_field . through . _meta . fields ) ) #NEWLINE# seen_to = sum ( ( ( to_model == getattr ( field . remote_field , 'model' , None ) ) for field in self . remote_field . through . _meta . fields ) ) #NEWLINE# if ( ( seen_from > 1 ) and ( not self . remote_field . through_fields ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "The model is used as an intermediate model by '%s', but it has more than one foreign key from '%s', which is ambiguous. You must specify which foreign key Django should use via the through_fields keyword argument." % ( self , from_model_name ) ) , hint = ( 'If you want to create a recursive relationship, use ForeignKey("self", symmetrical=False, through="%s").' % relationship_model_name ) , obj = self , id = 'fields.E334' ) ) #NEWLINE# #UNINDENT# if ( ( seen_to > 1 ) and ( not self . remote_field . through_fields ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "The model is used as an intermediate model by '%s', but it has more than one foreign key to '%s', which is ambiguous. You must specify which foreign key Django should use via the through_fields keyword argument." % ( self , to_model_name ) ) , hint = ( 'If you want to create a recursive relationship, use ForeignKey("self", symmetrical=False, through="%s").' % relationship_model_name ) , obj = self , id = 'fields.E335' ) ) #NEWLINE# #UNINDENT# if ( ( seen_from == 0 ) or ( seen_to == 0 ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "The model is used as an intermediate model by '%s', but it does not have a foreign key to '%s' or '%s'." % ( self , from_model_name , to_model_name ) ) , obj = self . remote_field . through , id = 'fields.E336' ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( self . remote_field . through_fields is not None ) : #NEWLINE# #INDENT# if ( not ( ( len ( self . remote_field . through_fields ) >= 2 ) and self . remote_field . through_fields [ 0 ] and self . remote_field . through_fields [ 1 ] ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "Field specifies 'through_fields' but does not provide the names of the two link fields that should be used for the relation through model '%s'." % qualified_model_name ) , hint = "Make sure you specify 'through_fields' as through_fields=('field1', 'field2')" , obj = self , id = 'fields.E337' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert ( from_model is not None ) , "ManyToManyField with intermediate tables cannot be checked if you don't pass the model where the field is attached to." #NEWLINE# ( source , through , target ) = ( from_model , self . remote_field . through , self . remote_field . model ) #NEWLINE# ( source_field_name , target_field_name ) = self . remote_field . through_fields [ : 2 ] #NEWLINE# for ( field_name , related_model ) in ( ( source_field_name , source ) , ( target_field_name , target ) ) : #NEWLINE# #INDENT# possible_field_names = [ ] #NEWLINE# for f in through . _meta . fields : #NEWLINE# #INDENT# if ( hasattr ( f , 'remote_field' ) and ( getattr ( f . remote_field , 'model' , None ) == related_model ) ) : #NEWLINE# #INDENT# possible_field_names . append ( f . name ) #NEWLINE# #UNINDENT# #UNINDENT# if possible_field_names : #NEWLINE# #INDENT# hint = ( "Did you mean one of the following foreign keys to '%s': %s?" % ( related_model . _meta . object_name , ', ' . join ( possible_field_names ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# hint = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# field = through . _meta . get_field ( field_name ) #NEWLINE# #UNINDENT# except exceptions . FieldDoesNotExist : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "The intermediary model '%s' has no field '%s'." % ( qualified_model_name , field_name ) ) , hint = hint , obj = self , id = 'fields.E338' ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not ( hasattr ( field , 'remote_field' ) and ( getattr ( field . remote_field , 'model' , None ) == qualified_model_name ) ) ) : #NEWLINE# #INDENT# errors . append ( checks . Error ( ( "'%s.%s' is not a foreign key to '%s'." % ( through . _meta . object_name , field_name , related_model . _meta . object_name ) ) , hint = hint , obj = self , id = 'fields.E339' ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return errors