diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
index 936d5e8..443312c 100644
a
|
b
|
from optparse import make_option
|
6 | 6 | |
7 | 7 | from django.core.management.base import NoArgsCommand, CommandError |
8 | 8 | from django.db import connections, DEFAULT_DB_ALIAS |
9 | | from django.utils import six |
| 9 | |
10 | 10 | |
11 | 11 | class Command(NoArgsCommand): |
12 | 12 | help = "Introspects the database tables in the given database and outputs a Django model module." |
… |
… |
class Command(NoArgsCommand):
|
34 | 34 | table_name_filter = options.get('table_name_filter') |
35 | 35 | |
36 | 36 | table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '') |
37 | | strip_prefix = lambda s: s.startswith("u'") and s[1:] or s |
| 37 | strip_prefix = lambda s: s[1:] if s.startswith("u'") else s |
38 | 38 | |
39 | 39 | cursor = connection.cursor() |
40 | 40 | yield "# This is an auto-generated Django model module." |
… |
… |
class Command(NoArgsCommand):
|
86 | 86 | extra_params['unique'] = True |
87 | 87 | |
88 | 88 | if is_relation: |
89 | | rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) |
| 89 | rel_to = "self" if relations[i][1] == table_name else table2model(relations[i][1]) |
90 | 90 | if rel_to in known_models: |
91 | 91 | field_type = 'ForeignKey(%s' % rel_to |
92 | 92 | else: |
diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
index 4a66214..a8adf86 100644
a
|
b
|
from django.db import models
|
3 | 3 | |
4 | 4 | class People(models.Model): |
5 | 5 | name = models.CharField(max_length=255) |
| 6 | parent = models.ForeignKey('self') |
6 | 7 | |
7 | 8 | class Message(models.Model): |
8 | 9 | from_field = models.ForeignKey(People, db_column='from_id') |
diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
index 028d263..f9cbbce 100644
a
|
b
|
class InspectDBTestCase(TestCase):
|
31 | 31 | stdout=out) |
32 | 32 | output = out.getvalue() |
33 | 33 | error_message = "inspectdb generated an attribute name which is a python keyword" |
| 34 | # Recursive foreign keys should be set to 'self' |
| 35 | self.assertIn("parent = models.ForeignKey('self')", output) |
34 | 36 | self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message) |
35 | 37 | # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted |
36 | 38 | self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')", |