Ticket #1328: management_rename.diff

File management_rename.diff, 2.2 KB (added by spam@…, 19 years ago)

Proposed solution

  • management.py

     
    583583        except NotImplementedError:
    584584            relations = {}
    585585        for i, row in enumerate(db.get_table_description(cursor, table_name)):
    586             column_name = row[0]
     586            field_name = column_name = row[0]
     587            if field_name in ('and', 'assert', 'break', 'class', 'continue', 'def', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield'):
     588                field_name = '%s_field'%(field_name)
     589               
    587590            if relations.has_key(i):
    588591                rel = relations[i]
    589592                rel_to = rel[1] == table_name and "'self'" or table2model(rel[1])
    590593                if column_name.endswith('_id'):
    591594                    field_desc = '%s = meta.ForeignKey(%s' % (column_name[:-3], rel_to)
    592595                else:
    593                     field_desc = '%s = meta.ForeignKey(%s, db_column=%r' % (column_name, rel_to, column_name)
     596                    field_desc = '%s = meta.ForeignKey(%s, db_column=%r' % (field_name, rel_to, column_name)
    594597            else:
    595598                try:
    596599                    field_type = db.DATA_TYPES_REVERSE[row[1]]
     
    607610                else:
    608611                    extra_params = {}
    609612
     613                if field_name != column_name:
     614                    extra_params['db_column'] = '%r'%column_name
     615                   
    610616                if field_type == 'CharField' and row[3]:
    611617                    extra_params['maxlength'] = row[3]
    612618
    613                 field_desc = '%s = meta.%s(' % (column_name, field_type)
     619                field_desc = '%s = meta.%s(' % (field_name, field_type)
    614620                field_desc += ', '.join(['%s=%s' % (k, v) for k, v in extra_params.items()])
    615621                field_desc += ')'
     622
     623                if field_name != column_name:
     624                    field_desc += ' # Db field was Python reserved word - field renamed'
     625
    616626                if field_type_was_guessed:
    617627                    field_desc += ' # This is a guess!'
    618628            yield '    %s' % field_desc
Back to Top