Ticket #19676: 19676-2.patch

File 19676-2.patch, 2.8 KB (added by aaugustin, 3 years ago)
  • django/core/management/commands/inspectdb.py

    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 
    66
    77from django.core.management.base import NoArgsCommand, CommandError
    88from django.db import connections, DEFAULT_DB_ALIAS
    9 from django.utils import six
     9
    1010
    1111class Command(NoArgsCommand):
    1212    help = "Introspects the database tables in the given database and outputs a Django model module."
    class Command(NoArgsCommand): 
    3434        table_name_filter = options.get('table_name_filter')
    3535
    3636        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
    3838
    3939        cursor = connection.cursor()
    4040        yield "# This is an auto-generated Django model module."
    class Command(NoArgsCommand): 
    8686                        extra_params['unique'] = True
    8787
    8888                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])
    9090                    if rel_to in known_models:
    9191                        field_type = 'ForeignKey(%s' % rel_to
    9292                    else:
  • tests/regressiontests/inspectdb/models.py

    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 
    33
    44class People(models.Model):
    55    name = models.CharField(max_length=255)
     6    parent = models.ForeignKey('self')
    67
    78class Message(models.Model):
    89    from_field = models.ForeignKey(People, db_column='from_id')
  • tests/regressiontests/inspectdb/tests.py

    diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
    index 028d263..f9cbbce 100644
    a b class InspectDBTestCase(TestCase): 
    3131                     stdout=out)
    3232        output = out.getvalue()
    3333        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)
    3436        self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", output, msg=error_message)
    3537        # As InspectdbPeople model is defined after InspectdbMessage, it should be quoted
    3638        self.assertIn("from_field = models.ForeignKey('InspectdbPeople', db_column='from_id')",
Back to Top