|
1 from django.core.management.base import NoArgsCommand, CommandError |
|
2 |
|
3 class Command(NoArgsCommand): |
|
4 help = "Introspects the database tables in the given database and outputs a Django model module." |
|
5 |
|
6 requires_model_validation = False |
|
7 |
|
8 def handle_noargs(self, **options): |
|
9 try: |
|
10 for line in self.handle_inspection(): |
|
11 print line |
|
12 except NotImplementedError: |
|
13 raise CommandError("Database inspection isn't supported for the currently selected database backend.") |
|
14 |
|
15 def handle_inspection(self): |
|
16 from django.db import connection, get_introspection_module |
|
17 import keyword |
|
18 |
|
19 introspection_module = get_introspection_module() |
|
20 |
|
21 table2model = lambda table_name: table_name.title().replace('_', '') |
|
22 |
|
23 cursor = connection.cursor() |
|
24 yield "# This is an auto-generated Django model module." |
|
25 yield "# You'll have to do the following manually to clean this up:" |
|
26 yield "# * Rearrange models' order" |
|
27 yield "# * Make sure each model has one field with primary_key=True" |
|
28 yield "# Feel free to rename the models, but don't rename db_table values or field names." |
|
29 yield "#" |
|
30 yield "# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'" |
|
31 yield "# into your database." |
|
32 yield '' |
|
33 yield 'from django.db import models' |
|
34 yield '' |
|
35 for table_name in introspection_module.get_table_list(cursor): |
|
36 yield 'class %s(models.Model):' % table2model(table_name) |
|
37 try: |
|
38 relations = introspection_module.get_relations(cursor, table_name) |
|
39 except NotImplementedError: |
|
40 relations = {} |
|
41 try: |
|
42 indexes = introspection_module.get_indexes(cursor, table_name) |
|
43 except NotImplementedError: |
|
44 indexes = {} |
|
45 for i, row in enumerate(introspection_module.get_table_description(cursor, table_name)): |
|
46 att_name = row[0].lower() |
|
47 comment_notes = [] # Holds Field notes, to be displayed in a Python comment. |
|
48 extra_params = {} # Holds Field parameters such as 'db_column'. |
|
49 |
|
50 if ' ' in att_name: |
|
51 extra_params['db_column'] = att_name |
|
52 att_name = att_name.replace(' ', '') |
|
53 comment_notes.append('Field renamed to remove spaces.') |
|
54 if keyword.iskeyword(att_name): |
|
55 extra_params['db_column'] = att_name |
|
56 att_name += '_field' |
|
57 comment_notes.append('Field renamed because it was a Python reserved word.') |
|
58 |
|
59 if i in relations: |
|
60 rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1]) |
|
61 field_type = 'ForeignKey(%s' % rel_to |
|
62 if att_name.endswith('_id'): |
|
63 att_name = att_name[:-3] |
|
64 else: |
|
65 extra_params['db_column'] = att_name |
|
66 else: |
|
67 try: |
|
68 field_type = introspection_module.DATA_TYPES_REVERSE[row[1]] |
|
69 except KeyError: |
|
70 field_type = 'TextField' |
|
71 comment_notes.append('This field type is a guess.') |
|
72 |
|
73 # This is a hook for DATA_TYPES_REVERSE to return a tuple of |
|
74 # (field_type, extra_params_dict). |
|
75 if type(field_type) is tuple: |
|
76 field_type, new_params = field_type |
|
77 extra_params.update(new_params) |
|
78 |
|
79 # Add max_length for all CharFields. |
|
80 if field_type == 'CharField' and row[3]: |
|
81 extra_params['max_length'] = row[3] |
|
82 |
|
83 if field_type == 'DecimalField': |
|
84 extra_params['max_digits'] = row[4] |
|
85 extra_params['decimal_places'] = row[5] |
|
86 |
|
87 # Add primary_key and unique, if necessary. |
|
88 column_name = extra_params.get('db_column', att_name) |
|
89 if column_name in indexes: |
|
90 if indexes[column_name]['primary_key']: |
|
91 extra_params['primary_key'] = True |
|
92 elif indexes[column_name]['unique']: |
|
93 extra_params['unique'] = True |
|
94 |
|
95 field_type += '(' |
|
96 |
|
97 # Don't output 'id = meta.AutoField(primary_key=True)', because |
|
98 # that's assumed if it doesn't exist. |
|
99 if att_name == 'id' and field_type == 'AutoField(' and extra_params == {'primary_key': True}: |
|
100 continue |
|
101 |
|
102 # Add 'null' and 'blank', if the 'null_ok' flag was present in the |
|
103 # table description. |
|
104 if row[6]: # If it's NULL... |
|
105 extra_params['blank'] = True |
|
106 if not field_type in ('TextField(', 'CharField('): |
|
107 extra_params['null'] = True |
|
108 |
|
109 field_desc = '%s = models.%s' % (att_name, field_type) |
|
110 if extra_params: |
|
111 if not field_desc.endswith('('): |
|
112 field_desc += ', ' |
|
113 field_desc += ', '.join(['%s=%r' % (k, v) for k, v in extra_params.items()]) |
|
114 field_desc += ')' |
|
115 if comment_notes: |
|
116 field_desc += ' # ' + ' '.join(comment_notes) |
|
117 yield ' %s' % field_desc |
|
118 yield ' class Meta:' |
|
119 yield ' db_table = %r' % table_name |
|
120 yield '' |