Ticket #12460: inspect_db_py_names.patch

File inspect_db_py_names.patch, 3.0 KB (added by Elijah Rutschman, 14 years ago)

Patch that addresses trailing underscore issue as well as several other introspection field naming scenarios

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

     
    1 import keyword
     1import keyword, re
    22from optparse import make_option
    33
    44from django.core.management.base import NoArgsCommand, CommandError
     
    5656                att_name = column_name.lower()
    5757                comment_notes = [] # Holds Field notes, to be displayed in a Python comment.
    5858                extra_params = {}  # Holds Field parameters such as 'db_column'.
     59                invalid_name_re = re.compile(r'([^a-zA-Z0-9_])') # Determines whether att_name is a valid Python name or not
    5960
    60                 # If the column name can't be used verbatim as a Python
    61                 # attribute, set the "db_column" for this Field.
    62                 if ' ' in att_name or '-' in att_name or keyword.iskeyword(att_name) or column_name != att_name:
    63                     extra_params['db_column'] = column_name
    64 
    6561                # Modify the field name to make it Python-compatible.
    66                 if ' ' in att_name:
    67                     att_name = att_name.replace(' ', '_')
    68                     comment_notes.append('Field renamed to remove spaces.')
    69                 if '-' in att_name:
    70                     att_name = att_name.replace('-', '_')
    71                     comment_notes.append('Field renamed to remove dashes.')
     62                invalid_chars = invalid_name_re.findall(att_name)
     63                for char in invalid_chars:
     64                    att_name = att_name.replace(char, '_')
     65                    comment_notes.append('Field renamed to remove character "%s".' % char)
     66                if att_name.startswith('_'):
     67                    att_name = 'field_' + att_name
     68                    comment_notes.append('Field renamed because it started with "_".')
     69                if att_name.endswith('_'):
     70                    att_name += '_field'
     71                    comment_notes.append('Field renamed because it ended with "_".')
     72                if att_name[0].isdigit():
     73                    att_name = 'field_' + att_name
     74                    comment_notes.append('Field renamed because it started with a digit.')
    7275                if keyword.iskeyword(att_name):
    7376                    att_name += '_field'
    7477                    comment_notes.append('Field renamed because it was a Python reserved word.')
     78                while att_name.find('__') >= 0:
     79                    att_name = att_name.replace('__', '_')
     80                    comment_notes.append('Field renamed because it contained "__".')
     81
     82                # If the column name can't be used verbatim as a Python
     83                # attribute, set the "db_column" for this Field.
    7584                if column_name != att_name:
     85                    extra_params['db_column'] = column_name
    7686                    comment_notes.append('Field name made lowercase.')
    7787
    7888                if i in relations:
Back to Top