Ticket #15076: django-fk-quoting.diff

File django-fk-quoting.diff, 1.3 KB (added by jeff@…, 4 years ago)

Quote ForeignKey models only if the model has not appeared yet.

  • django/core/management/commands/inspectdb.py

    diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
    index e45f22c..2db7a85 100644
    a b class Command(NoArgsCommand): 
    4141        yield ''
    4242        yield 'from %s import models' % self.db_module
    4343        yield ''
     44        known_models = []
    4445        for table_name in connection.introspection.get_table_list(cursor):
    4546            yield 'class %s(models.Model):' % table2model(table_name)
     47            known_models.append(table2model(table_name))
    4648            try:
    4749                relations = connection.introspection.get_relations(cursor, table_name)
    4850            except NotImplementedError:
    class Command(NoArgsCommand): 
    7779
    7880                if i in relations:
    7981                    rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1])
    80                     field_type = 'ForeignKey(%s' % rel_to
     82
     83                    if rel_to in known_models:
     84                        field_type = 'ForeignKey(%s' % rel_to
     85                    else:
     86                        field_type = "ForeignKey('%s'" % rel_to
     87
    8188                    if att_name.endswith('_id'):
    8289                        att_name = att_name[:-3]
    8390                    else:
Back to Top