From b144bfb5ce1e3e283c7fddac29a4653c6bc7fb3f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 5 Jul 2014 20:48:57 +0200 Subject: [PATCH] Fixed #21079 -- Further normalized table names in inspectdb Thanks Michael Manfre for the report and Tim Graham for the review. --- django/core/management/commands/inspectdb.py | 2 +- tests/inspectdb/models.py | 5 ++++- tests/inspectdb/tests.py | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 7feef23b08c..91aa3255d77 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -32,7 +32,7 @@ class Command(BaseCommand): # 'table_name_filter' is a stealth option table_name_filter = options.get('table_name_filter') - table2model = lambda table_name: table_name.title().replace('_', '').replace(' ', '').replace('-', '') + table2model = lambda table_name: re.sub(r'[^a-zA-Z0-9]', '', table_name.title()) strip_prefix = lambda s: s[1:] if s.startswith("u'") else s with connection.cursor() as cursor: diff --git a/tests/inspectdb/models.py b/tests/inspectdb/models.py index f7c8af73c6f..6b55ca78819 100644 --- a/tests/inspectdb/models.py +++ b/tests/inspectdb/models.py @@ -30,7 +30,7 @@ class DigitsInColumnName(models.Model): leading_digits = models.CharField(max_length=11, db_column='45extra') -class SpecialColumnName(models.Model): +class SpecialName(models.Model): field = models.IntegerField(db_column='field') # Underscores field_field_0 = models.IntegerField(db_column='Field_') @@ -40,6 +40,9 @@ class SpecialColumnName(models.Model): prc_x = models.IntegerField(db_column='prc(%) x') non_ascii = models.IntegerField(db_column='tamaƱo') + class Meta: + db_table = "inspectdb_special.table name" + class ColumnTypes(models.Model): id = models.AutoField(primary_key=True) diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index c0502198d85..2fa4e25453a 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -179,7 +179,9 @@ class InspectDBTestCase(TestCase): unsuitable for Python identifiers """ out = StringIO() - call_command('inspectdb', stdout=out) + call_command('inspectdb', + table_name_filter=lambda tn: tn.startswith('inspectdb_'), + stdout=out) output = out.getvalue() base_name = 'Field' if not connection.features.uppercases_column_names else 'field' self.assertIn("field = models.IntegerField()", output) @@ -193,6 +195,18 @@ class InspectDBTestCase(TestCase): else: self.assertIn("tama_o = models.IntegerField(db_column='tama\\xf1o')", output) + def test_table_name_introspection(self): + """ + Introspection of table names containing special characters, + unsuitable for Python identifiers + """ + out = StringIO() + call_command('inspectdb', + table_name_filter=lambda tn: tn.startswith('inspectdb_'), + stdout=out) + output = out.getvalue() + self.assertIn("class InspectdbSpecialTableName(models.Model):", output) + def test_managed_models(self): """Test that by default the command generates models with `Meta.managed = False` (#14305)""" out = StringIO()