mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Added first stab at 'django-admin.py inspectdb', which takes a database name and introspects the tables, outputting a Django model. Works in PostgreSQL and MySQL. It's missing some niceties at the moment, such as detection of primary-keys and relationships, but it works. Refs #90.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@384 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -430,6 +430,32 @@ def createsuperuser():
|
||||
print "User created successfully."
|
||||
createsuperuser.args = ''
|
||||
|
||||
def inspectdb(db_name):
|
||||
"Generator that introspects the tables in the given database name and returns a Django model, one line at a time."
|
||||
from django.core import db
|
||||
from django.conf import settings
|
||||
settings.DATABASE_NAME = db_name
|
||||
cursor = db.db.cursor()
|
||||
yield 'from django.core import meta'
|
||||
yield ''
|
||||
for table_name in db.get_table_list(cursor):
|
||||
object_name = table_name.title().replace('_', '')
|
||||
object_name = object_name.endswith('s') and object_name[:-1] or object_name
|
||||
yield 'class %s(meta.Model):' % object_name
|
||||
yield ' db_table = %r' % table_name
|
||||
yield ' fields = ('
|
||||
cursor.execute("SELECT * FROM %s LIMIT 1" % table_name)
|
||||
for row in cursor.description:
|
||||
field_type = db.DATA_TYPES_REVERSE[row[1]]
|
||||
field_desc = 'meta.%s(%r' % (field_type, row[0])
|
||||
if field_type == 'CharField':
|
||||
field_desc += ', maxlength=%s' % (row[3])
|
||||
yield ' %s),' % field_desc
|
||||
yield ' )'
|
||||
yield ''
|
||||
inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module."
|
||||
inspectdb.args = "[dbname]"
|
||||
|
||||
def runserver(port):
|
||||
"Starts a lightweight Web server for development."
|
||||
from django.core.servers.basehttp import run, WSGIServerException
|
||||
|
||||
Reference in New Issue
Block a user