Author: claudep
Date: 2012-04-26 23:56:31 -0700 (Thu, 26 Apr 2012)
New Revision: 17942

Modified:
   django/trunk/django/core/management/commands/inspectdb.py
   django/trunk/tests/regressiontests/inspectdb/tests.py
Log:
Fixed #15076 -- Quoted ForeignKey target class names in inspectdb when class is 
defined below.

Thanks saschwarz for the report, [email protected] for the initial 
patch and Ramiro Morales for the review. 


Modified: django/trunk/django/core/management/commands/inspectdb.py
===================================================================
--- django/trunk/django/core/management/commands/inspectdb.py   2012-04-26 
19:56:20 UTC (rev 17941)
+++ django/trunk/django/core/management/commands/inspectdb.py   2012-04-27 
06:56:31 UTC (rev 17942)
@@ -41,8 +41,10 @@
         yield ''
         yield 'from %s import models' % self.db_module
         yield ''
+        known_models = []
         for table_name in connection.introspection.get_table_list(cursor):
             yield 'class %s(models.Model):' % table2model(table_name)
+            known_models.append(table2model(table_name))
             try:
                 relations = connection.introspection.get_relations(cursor, 
table_name)
             except NotImplementedError:
@@ -83,7 +85,12 @@
 
                 if i in relations:
                     rel_to = relations[i][1] == table_name and "'self'" or 
table2model(relations[i][1])
-                    field_type = 'ForeignKey(%s' % rel_to
+
+                    if rel_to in known_models:
+                        field_type = 'ForeignKey(%s' % rel_to
+                    else:
+                        field_type = "ForeignKey('%s'" % rel_to
+
                     if att_name.endswith('_id'):
                         att_name = att_name[:-3]
                     else:

Modified: django/trunk/tests/regressiontests/inspectdb/tests.py
===================================================================
--- django/trunk/tests/regressiontests/inspectdb/tests.py       2012-04-26 
19:56:20 UTC (rev 17941)
+++ django/trunk/tests/regressiontests/inspectdb/tests.py       2012-04-27 
06:56:31 UTC (rev 17942)
@@ -12,7 +12,8 @@
         call_command('inspectdb', stdout=out)
         error_message = "inspectdb generated an attribute name which is a 
python keyword"
         self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", 
out.getvalue(), msg=error_message)
-        self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", 
out.getvalue())
+        # As InspectdbPeople model is defined after InspectdbMessage, it 
should be quoted
+        self.assertIn("from_field = models.ForeignKey('InspectdbPeople')", 
out.getvalue())
         self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, 
primary_key=True)",
             out.getvalue())
         self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, 
unique=True)",

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to