Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed 14796 -- Modified order of operations performed on field names …

…by the inspectdb command so it doesn't generates model fields with names equal to Python keywords. Thanks pappjm at gmail dot com for the report and mmcnickle for the fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15296 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bafe879188f3cd268f81e426bcdf4addc2d9a10a 1 parent 3f528e1
Ramiro Morales ramiro authored
11 django/core/management/commands/inspectdb.py
View
@@ -20,7 +20,7 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options):
try:
for line in self.handle_inspection(options):
- print line
+ self.stdout.write("%s\n" % line)
except NotImplementedError:
raise CommandError("Database inspection isn't supported for the currently selected database backend.")
@@ -66,12 +66,11 @@ def handle_inspection(self, options):
if ' ' in att_name:
att_name = att_name.replace(' ', '_')
comment_notes.append('Field renamed to remove spaces.')
+
if '-' in att_name:
att_name = att_name.replace('-', '_')
comment_notes.append('Field renamed to remove dashes.')
- if keyword.iskeyword(att_name):
- att_name += '_field'
- comment_notes.append('Field renamed because it was a Python reserved word.')
+
if column_name != att_name:
comment_notes.append('Field name made lowercase.')
@@ -97,6 +96,10 @@ def handle_inspection(self, options):
extra_params['unique'] = True
field_type += '('
+
+ if keyword.iskeyword(att_name):
+ att_name += '_field'
+ comment_notes.append('Field renamed because it was a Python reserved word.')
# Don't output 'id = meta.AutoField(primary_key=True)', because
# that's assumed if it doesn't exist.
1  tests/regressiontests/inspectdb/__init__.py
View
@@ -0,0 +1 @@
+
1  tests/regressiontests/inspectdb/bug/__init__.py
View
@@ -0,0 +1 @@
+
7 tests/regressiontests/inspectdb/bug/models.py
View
@@ -0,0 +1,7 @@
+from django.db import models
+
+class People(models.Model):
+ name = models.CharField(max_length=255)
+
+class Message(models.Model):
+ from_field = models.ForeignKey(People, db_column='from_id')
1  tests/regressiontests/inspectdb/models.py
View
@@ -0,0 +1 @@
+
29 tests/regressiontests/inspectdb/tests.py
View
@@ -0,0 +1,29 @@
+import os
+import sys
+from StringIO import StringIO
+
+from django.conf import settings
+from django.core.management import call_command
+from django.db.models.loading import load_app
+from django.test import TestCase
+
+class InspectDBTestCase(TestCase):
+
+ def setUp(self):
+ self.old_sys_path = sys.path[:]
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
+ self.old_installed_apps = settings.INSTALLED_APPS
+ settings.INSTALLED_APPS = ('bug',)
+ map(load_app, settings.INSTALLED_APPS)
+ call_command('syncdb', verbosity=0)
+
+ def test_attribute_name_not_python_keyword(self):
+ out = StringIO()
+ call_command('inspectdb', stdout=out)
+ error_message = "inspectdb generated an attribute name which is a python keyword"
+ self.assertNotIn("from = models.ForeignKey(BugPeople)", out.getvalue(), msg=error_message)
+ out.close()
+
+ def tearDown(self):
+ settings.INSTALLED_APPS = self.old_installed_apps
+ sys.path = self.old_sys_path
Please sign in to comment.
Something went wrong with that request. Please try again.