Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include LICENSE README.rst
recursive-include openlayers/templates *.txt *.html *.xml *.js
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ Just use it as the normal `inspectdb management command`_::

python manage.py inspectdb > models.py

The following command-line switches are available:

-d,--database

python manage.py inspectdb -d yourdbname > models.py

-t,--tables

python manage.py inspectdb -t yourtablename1,yourtablename2 > models.py

-e,--exclude-tables

python manage.py inspectdb -e yourtablename1,yourtablename2 > models.py


.. _ticket #5725: http://code.djangoproject.com/ticket/5725
.. _inspectdb management command: http://docs.djangoproject.com/en/1.3/howto/legacy-databases/#auto-generate-the-models
Expand Down
41 changes: 40 additions & 1 deletion inspectdb/management/commands/inspectdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ class Command(NoArgsCommand):
default=False,
help=('Forces the field key word argument ``db_column``'),
),
make_option(
'-t',
'--tables',
action='store',
dest='selected_tables',
default=None,
help=('Inspects tables in list (comma separated)'),
),
make_option(
'-e',
'--excluded-tables',
action='store',
dest='excluded_tables',
default=None,
help=('Do not inspect tables in list (comma separated)'),
),
)

requires_model_validation = False
Expand All @@ -43,11 +59,34 @@ def handle_noargs(self, **options):
def handle_inspection(self, options):
alias = options['database']
force_db_column = options['force_db_column']
selected_tables = options['selected_tables']
excluded_tables = options['excluded_tables']
self.engine = settings.DATABASES[alias]['ENGINE']
self.connection = connections[alias]
cursor = self.connection.cursor()
yield 'from %s import models' % self.db_module
for table_name in self.connection.introspection.get_table_list(cursor):

all_tables_list = self.connection.introspection.get_table_list(cursor)[:]
if selected_tables is None and excluded_tables is None:
selected_tables_list = all_tables_list
elif selected_tables and len(selected_tables):
try:
selected_tables_list = selected_tables.split(",")
selected_tables_list = [x.strip() for x in selected_tables_list]
tables_not_found = [t for t in selected_tables_list if t not in all_tables_list]
if len(tables_not_found):
raise
except:
raise CommandError("With the -t or --tables, you must provide a comma separated list of existing tables")
elif excluded_tables and len(excluded_tables):
try:
excluded_tables_list = excluded_tables.split(",")
excluded_tables_list = [x.strip() for x in excluded_tables_list]
selected_tables_list = [x for x in all_tables_list if x not in excluded_tables_list]
except Exception, e:
raise CommandError("With the -e or --excluded-tables, you must provide a comma separated list of tables")

for table_name in selected_tables_list:
yield ''
yield ''
yield 'class %s(models.Model):' % to_model(table_name)
Expand Down