Changed django-admin.py inspectdb to output new-style model syntax
git-svn-id: http://code.djangoproject.com/svn/django/trunk@550 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
25264c8604
commit
939e2bccd5
|
@ -455,30 +455,33 @@ def inspectdb(db_name):
|
||||||
yield ''
|
yield ''
|
||||||
for table_name in db.get_table_list(cursor):
|
for table_name in db.get_table_list(cursor):
|
||||||
yield 'class %s(meta.Model):' % table2model(table_name)
|
yield 'class %s(meta.Model):' % table2model(table_name)
|
||||||
yield ' db_table = %r' % table_name
|
|
||||||
yield ' fields = ('
|
|
||||||
try:
|
try:
|
||||||
relations = db.get_relations(cursor, table_name)
|
relations = db.get_relations(cursor, table_name)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
relations = {}
|
relations = {}
|
||||||
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
|
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
|
||||||
for i, row in enumerate(cursor.description):
|
for i, row in enumerate(cursor.description):
|
||||||
|
column_name = row[0]
|
||||||
if relations.has_key(i):
|
if relations.has_key(i):
|
||||||
rel = relations[i]
|
rel = relations[i]
|
||||||
rel_to = rel[1] == table_name and "'self'" or table2model(rel[1])
|
rel_to = rel[1] == table_name and "'self'" or table2model(rel[1])
|
||||||
field_desc = 'meta.ForeignKey(%s, name=%r' % (rel_to, row[0])
|
if column_name.endswith('_id'):
|
||||||
|
field_desc = '%s = meta.ForeignKey(%s' % (column_name[:-3], rel_to)
|
||||||
|
else:
|
||||||
|
field_desc = '%s = meta.ForeignKey(%s, db_column=%r' % (column_name, rel_to, column_name)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
field_type = db.DATA_TYPES_REVERSE[row[1]]
|
field_type = db.DATA_TYPES_REVERSE[row[1]]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
field_type = 'TextField'
|
field_type = 'TextField'
|
||||||
yield " # The model-creator script used TextField by default, because"
|
yield " # The model-creator script used TextField by default, because"
|
||||||
yield " # it couldn't recognize your field type."
|
yield " # it couldn't recognize your field type."
|
||||||
field_desc = 'meta.%s(%r' % (field_type, row[0])
|
field_desc = '%s = meta.%s(' % (column_name, field_type)
|
||||||
if field_type == 'CharField':
|
if field_type == 'CharField':
|
||||||
field_desc += ', maxlength=%s' % (row[3])
|
field_desc += 'maxlength=%s' % (row[3])
|
||||||
yield ' %s),' % field_desc
|
yield ' %s)' % field_desc
|
||||||
yield ' )'
|
yield ' class META:'
|
||||||
|
yield ' db_table = %r' % table_name
|
||||||
yield ''
|
yield ''
|
||||||
inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module."
|
inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module."
|
||||||
inspectdb.args = "[dbname]"
|
inspectdb.args = "[dbname]"
|
||||||
|
|
Loading…
Reference in New Issue