File tree Expand file tree Collapse file tree 2 files changed +10
-2
lines changed
django/core/management/commands
tests/regressiontests/inspectdb Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -41,8 +41,10 @@ def handle_inspection(self, options):
41
41
yield ''
42
42
yield 'from %s import models' % self .db_module
43
43
yield ''
44
+ known_models = []
44
45
for table_name in connection .introspection .get_table_list (cursor ):
45
46
yield 'class %s(models.Model):' % table2model (table_name )
47
+ known_models .append (table2model (table_name ))
46
48
try :
47
49
relations = connection .introspection .get_relations (cursor , table_name )
48
50
except NotImplementedError :
@@ -83,7 +85,12 @@ def handle_inspection(self, options):
83
85
84
86
if i in relations :
85
87
rel_to = relations [i ][1 ] == table_name and "'self'" or table2model (relations [i ][1 ])
86
- field_type = 'ForeignKey(%s' % rel_to
88
+
89
+ if rel_to in known_models :
90
+ field_type = 'ForeignKey(%s' % rel_to
91
+ else :
92
+ field_type = "ForeignKey('%s'" % rel_to
93
+
87
94
if att_name .endswith ('_id' ):
88
95
att_name = att_name [:- 3 ]
89
96
else :
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ def test_attribute_name_not_python_keyword(self):
12
12
call_command ('inspectdb' , stdout = out )
13
13
error_message = "inspectdb generated an attribute name which is a python keyword"
14
14
self .assertNotIn ("from = models.ForeignKey(InspectdbPeople)" , out .getvalue (), msg = error_message )
15
- self .assertIn ("from_field = models.ForeignKey(InspectdbPeople)" , out .getvalue ())
15
+ # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
16
+ self .assertIn ("from_field = models.ForeignKey('InspectdbPeople')" , out .getvalue ())
16
17
self .assertIn ("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)" ,
17
18
out .getvalue ())
18
19
self .assertIn ("people_unique = models.ForeignKey(InspectdbPeople, unique=True)" ,
You can’t perform that action at this time.
0 commit comments