Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

magic-removal: Fixed #1544 -- Changed 'inspectdb' to use database nam…

…e from DATABASE_NAME setting instead of command line. Thanks, pb

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 32aa5c9 commit ef3c0e06e178272d49639b4dc5b6ff280955f192 @adrianholovaty adrianholovaty committed
11 django/core/
@@ -668,7 +668,7 @@ def startapp(app_name, directory):
startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
startapp.args = "[appname]"
-def inspectdb(db_name):
+def inspectdb():
"Generator that introspects the tables in the given database name and returns a Django model, one line at a time."
from django.db import connection, get_introspection_module
from django.conf import settings
@@ -680,7 +680,6 @@ def table2model(table_name):
object_name = table_name.title().replace('_', '')
return object_name.endswith('s') and object_name[:-1] or object_name
- settings.DATABASE_NAME = db_name
cursor = connection.cursor()
yield "# This is an auto-generated Django model module."
yield "# You'll have to do the following manually to clean this up:"
@@ -776,7 +775,7 @@ def table2model(table_name):
yield ' db_table = %r' % table_name
yield ''
inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module."
-inspectdb.args = "[dbname]"
+inspectdb.args = ""
class ModelErrorCollection:
def __init__(self, outfile=sys.stdout):
@@ -1153,11 +1152,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
elif action == 'inspectdb':
- param = args[1]
- except IndexError:
- parser.print_usage_and_exit()
- try:
- for line in action_mapping[action](param):
+ for line in action_mapping[action]():
print line
except NotImplementedError:
sys.stderr.write(style.ERROR("Error: %r isn't supported for the currently selected database backend.\n" % action))
16 docs/django-admin.txt
@@ -91,11 +91,11 @@ example, the default settings don't define ``ROOT_URLCONF``, so
Note that Django's default settings live in ``django/conf/``.
-inspectdb [dbname]
-Introspects the database tables in the given database and outputs a Django
-model module to standard output.
+Introspects the database tables in the database pointed-to by the
+``DATABASE_NAME`` setting and outputs a Django model module to standard output.
Use this if you have a legacy database with which you'd like to use Django.
The script will inspect the database and create a model for each table within
@@ -124,13 +124,11 @@ you run it, you'll want to look over the generated models yourself to make
customizations. In particular, you'll need to rearrange models' order, so that
models that refer to other models are ordered properly.
-If you're using Django 0.90 or 0.91, you'll need to add ``primary_key=True`` to
-one field in each model. In the Django development version, primary keys are
-automatically introspected for PostgreSQL and MySQL, and Django puts in the
-``primary_key=True`` where needed.
+Primary keys are automatically introspected for PostgreSQL and MySQL, and
+Django puts in the ``primary_key=True`` where needed.
``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
-only works in PostgreSQL.
+only works in PostgreSQL and with certain types of MySQL tables.
install [modelmodule modelmodule ...]
8 docs/legacy_databases.txt
@@ -18,6 +18,7 @@ You'll need to tell Django what your database connection parameters are, and
what the name of the database is. Do that by editing these settings in your
`settings file`_:
@@ -26,6 +27,7 @@ what the name of the database is. Do that by editing these settings in your
.. _settings file:
@@ -39,13 +41,11 @@ Auto-generate the models
Django comes with a utility that can create models by introspecting an existing
database. You can view the output by running this command::
- inspectdb [databasename]
-...where "[databasename]" is the name of your database.
+ inspectdb
Save this as a file by using standard Unix output redirection::
- inspectdb [databasename] >
+ inspectdb >
This feature is meant as a shortcut, not as definitive model generation. See
the ` documentation`_ for more information.

0 comments on commit ef3c0e0

Please sign in to comment.
Something went wrong with that request. Please try again.