diff --git a/MANIFEST.in b/MANIFEST.in index 8dce522..6979822 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ include LICENSE README.rst +recursive-include openlayers/templates *.txt *.html *.xml *.js \ No newline at end of file diff --git a/README.rst b/README.rst index 429b606..6309b5c 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/inspectdb/management/commands/inspectdb.py b/inspectdb/management/commands/inspectdb.py index a1a85dd..c6ee320 100644 --- a/inspectdb/management/commands/inspectdb.py +++ b/inspectdb/management/commands/inspectdb.py @@ -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 @@ -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)