Ticket #12460: inspect_db_py_names.patch
File inspect_db_py_names.patch, 3.0 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/inspectdb.py
1 import keyword 1 import keyword, re 2 2 from optparse import make_option 3 3 4 4 from django.core.management.base import NoArgsCommand, CommandError … … 56 56 att_name = column_name.lower() 57 57 comment_notes = [] # Holds Field notes, to be displayed in a Python comment. 58 58 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 59 60 60 # If the column name can't be used verbatim as a Python61 # 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_name64 65 61 # 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.') 72 75 if keyword.iskeyword(att_name): 73 76 att_name += '_field' 74 77 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. 75 84 if column_name != att_name: 85 extra_params['db_column'] = column_name 76 86 comment_notes.append('Field name made lowercase.') 77 87 78 88 if i in relations: