Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored January 24, 2011
11  django/core/management/commands/inspectdb.py
@@ -20,7 +20,7 @@ class Command(NoArgsCommand):
20 20
     def handle_noargs(self, **options):
21 21
         try:
22 22
             for line in self.handle_inspection(options):
23  
-                print line
  23
+                self.stdout.write("%s\n" % line)
24 24
         except NotImplementedError:
25 25
             raise CommandError("Database inspection isn't supported for the currently selected database backend.")
26 26
 
@@ -66,12 +66,11 @@ def handle_inspection(self, options):
66 66
                 if ' ' in att_name:
67 67
                     att_name = att_name.replace(' ', '_')
68 68
                     comment_notes.append('Field renamed to remove spaces.')
  69
+                    
69 70
                 if '-' in att_name:
70 71
                     att_name = att_name.replace('-', '_')
71 72
                     comment_notes.append('Field renamed to remove dashes.')
72  
-                if keyword.iskeyword(att_name):
73  
-                    att_name += '_field'
74  
-                    comment_notes.append('Field renamed because it was a Python reserved word.')
  73
+                    
75 74
                 if column_name != att_name:
76 75
                     comment_notes.append('Field name made lowercase.')
77 76
 
@@ -97,6 +96,10 @@ def handle_inspection(self, options):
97 96
                             extra_params['unique'] = True
98 97
 
99 98
                     field_type += '('
  99
+                    
  100
+                if keyword.iskeyword(att_name):
  101
+                    att_name += '_field'
  102
+                    comment_notes.append('Field renamed because it was a Python reserved word.')
100 103
 
101 104
                 # Don't output 'id = meta.AutoField(primary_key=True)', because
102 105
                 # that's assumed if it doesn't exist.
1  tests/regressiontests/inspectdb/__init__.py
... ...
@@ -0,0 +1 @@
  1
+
1  tests/regressiontests/inspectdb/bug/__init__.py
... ...
@@ -0,0 +1 @@
  1
+
7  tests/regressiontests/inspectdb/bug/models.py
... ...
@@ -0,0 +1,7 @@
  1
+from django.db import models
  2
+
  3
+class People(models.Model):
  4
+    name = models.CharField(max_length=255)
  5
+
  6
+class Message(models.Model):
  7
+    from_field = models.ForeignKey(People, db_column='from_id')
1  tests/regressiontests/inspectdb/models.py
... ...
@@ -0,0 +1 @@
  1
+
29  tests/regressiontests/inspectdb/tests.py
... ...
@@ -0,0 +1,29 @@
  1
+import os
  2
+import sys
  3
+from StringIO import StringIO
  4
+
  5
+from django.conf import settings
  6
+from django.core.management import call_command
  7
+from django.db.models.loading import load_app
  8
+from django.test import TestCase
  9
+
  10
+class InspectDBTestCase(TestCase):
  11
+    
  12
+    def setUp(self):
  13
+        self.old_sys_path = sys.path[:]
  14
+        sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  15
+        self.old_installed_apps = settings.INSTALLED_APPS
  16
+        settings.INSTALLED_APPS = ('bug',)
  17
+        map(load_app, settings.INSTALLED_APPS)
  18
+        call_command('syncdb', verbosity=0)
  19
+        
  20
+    def test_attribute_name_not_python_keyword(self):
  21
+        out = StringIO()
  22
+        call_command('inspectdb', stdout=out)
  23
+        error_message = "inspectdb generated an attribute name which is a python keyword"
  24
+        self.assertNotIn("from = models.ForeignKey(BugPeople)", out.getvalue(), msg=error_message)
  25
+        out.close()
  26
+        
  27
+    def tearDown(self):
  28
+        settings.INSTALLED_APPS = self.old_installed_apps
  29
+        sys.path = self.old_sys_path

0 notes on commit bafe879

Please sign in to comment.
Something went wrong with that request. Please try again.