Loading django/core/management/commands/inspectdb.py +3 −3 Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ from optparse import make_option from django.core.management.base import NoArgsCommand, CommandError from django.db import connections, DEFAULT_DB_ALIAS from django.utils import six class Command(NoArgsCommand): help = "Introspects the database tables in the given database and outputs a Django model module." Loading Loading @@ -34,7 +34,7 @@ class Command(NoArgsCommand): table_name_filter = options.get('table_name_filter') table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '') strip_prefix = lambda s: s.startswith("u'") and s[1:] or s strip_prefix = lambda s: s[1:] if s.startswith("u'") else s cursor = connection.cursor() yield "# This is an auto-generated Django model module." Loading Loading @@ -86,7 +86,7 @@ class Command(NoArgsCommand): extra_params['unique'] = True if is_relation: rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) rel_to = "self" if relations[i][1] == table_name else table2model(relations[i][1]) if rel_to in known_models: field_type = 'ForeignKey(%s' % rel_to else: Loading tests/regressiontests/inspectdb/models.py +1 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from django.db import models class People(models.Model): name = models.CharField(max_length=255) parent = models.ForeignKey('self') class Message(models.Model): from_field = models.ForeignKey(People, db_column='from_id') Loading tests/regressiontests/inspectdb/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,8 @@ class InspectDBTestCase(TestCase): stdout=out) output = out.getvalue() error_message = "inspectdb generated an attribute name which is a python keyword" # Recursive foreign keys should be set to 'self' self.assertIn("parent = models.ForeignKey('self')", output) self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message) # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')", Loading Loading
django/core/management/commands/inspectdb.py +3 −3 Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ from optparse import make_option from django.core.management.base import NoArgsCommand, CommandError from django.db import connections, DEFAULT_DB_ALIAS from django.utils import six class Command(NoArgsCommand): help = "Introspects the database tables in the given database and outputs a Django model module." Loading Loading @@ -34,7 +34,7 @@ class Command(NoArgsCommand): table_name_filter = options.get('table_name_filter') table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '') strip_prefix = lambda s: s.startswith("u'") and s[1:] or s strip_prefix = lambda s: s[1:] if s.startswith("u'") else s cursor = connection.cursor() yield "# This is an auto-generated Django model module." Loading Loading @@ -86,7 +86,7 @@ class Command(NoArgsCommand): extra_params['unique'] = True if is_relation: rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) rel_to = "self" if relations[i][1] == table_name else table2model(relations[i][1]) if rel_to in known_models: field_type = 'ForeignKey(%s' % rel_to else: Loading
tests/regressiontests/inspectdb/models.py +1 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from django.db import models class People(models.Model): name = models.CharField(max_length=255) parent = models.ForeignKey('self') class Message(models.Model): from_field = models.ForeignKey(People, db_column='from_id') Loading
tests/regressiontests/inspectdb/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,8 @@ class InspectDBTestCase(TestCase): stdout=out) output = out.getvalue() error_message = "inspectdb generated an attribute name which is a python keyword" # Recursive foreign keys should be set to 'self' self.assertIn("parent = models.ForeignKey('self')", output) self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message) # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')", Loading