Skip to content

Commit

Permalink
magic-removal: Fixed #1544 -- Changed 'inspectdb' to use database nam…
Browse files Browse the repository at this point in the history
…e from DATABASE_NAME setting instead of command line. Thanks, pb

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2711 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Apr 17, 2006
1 parent 32aa5c9 commit ef3c0e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
11 changes: 3 additions & 8 deletions django/core/management.py
Expand Up @@ -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
Expand All @@ -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:"
Expand Down Expand 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):
Expand Down Expand Up @@ -1153,11 +1152,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
action_mapping[action]()
elif action == 'inspectdb':
try:
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))
Expand Down
16 changes: 7 additions & 9 deletions docs/django-admin.txt
Expand Up @@ -91,11 +91,11 @@ example, the default settings don't define ``ROOT_URLCONF``, so

Note that Django's default settings live in ``django/conf/global_settings.py``.

inspectdb [dbname]
------------------
inspectdb
---------

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
Expand Down Expand Up @@ -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 ...]
-------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions docs/legacy_databases.txt
Expand Up @@ -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`_:

* `DATABASE_NAME`
* `DATABASE_ENGINE`_
* `DATABASE_USER`_
* `DATABASE_PASSWORD`_
Expand All @@ -26,6 +27,7 @@ what the name of the database is. Do that by editing these settings in your
* `DATABASE_PORT`_

.. _settings file: http://www.djangoproject.com/documentation/settings/
.. _DATABASE_NAME: http://www.djangoproject.com/documentation/settings/#database-name
.. _DATABASE_ENGINE: http://www.djangoproject.com/documentation/settings/#database-engine
.. _DATABASE_USER: http://www.djangoproject.com/documentation/settings/#database-user
.. _DATABASE_PASSWORD: http://www.djangoproject.com/documentation/settings/#database-password
Expand All @@ -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::

django-admin.py inspectdb [databasename] --settings=path.to.settings

...where "[databasename]" is the name of your database.
django-admin.py inspectdb --settings=path.to.settings

Save this as a file by using standard Unix output redirection::

django-admin.py inspectdb [databasename] --settings=path.to.settings > appname.py
django-admin.py inspectdb --settings=path.to.settings > appname.py

This feature is meant as a shortcut, not as definitive model generation. See
the `django-admin.py documentation`_ for more information.
Expand Down

0 comments on commit ef3c0e0

Please sign in to comment.