From 39a16dd2e003f9ca82a6d061a022ced6904bd243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Tom=C3=A1s=20Tocino?= Date: Mon, 2 Nov 2015 00:53:43 +0100 Subject: [PATCH] Fixed #25658 -- Allowed inspectdb to inspect a specific set of tables. --- AUTHORS | 1 + django/core/management/commands/inspectdb.py | 6 +++++- docs/ref/django-admin.txt | 10 ++++++++-- docs/releases/1.10.txt | 3 +++ tests/inspectdb/tests.py | 11 +++++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index f9c90f9359..d7422a0767 100644 --- a/AUTHORS +++ b/AUTHORS @@ -373,6 +373,7 @@ answer newbie questions, and generally made Django that much better: Jordan Dimov Jorge Bastida Jorge Gajon + José Tomás Tocino García Joseph Kocherhans Josh Smeaton Joshua Ginsberg diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 982ba7f1bd..24310ee390 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -17,6 +17,8 @@ class Command(BaseCommand): db_module = 'django.db' def add_arguments(self, parser): + parser.add_argument('table', action='store', nargs='*', type=str, + help='Selects what tables or views should be introspected.') parser.add_argument('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to ' 'introspect. Defaults to using the "default" database.') @@ -54,7 +56,9 @@ class Command(BaseCommand): yield '' yield 'from %s import models' % self.db_module known_models = [] - for table_name in connection.introspection.table_names(cursor): + tables_to_introspect = options['table'] or connection.introspection.table_names(cursor) + + for table_name in tables_to_introspect: if table_name_filter is not None and callable(table_name_filter): if not table_name_filter(table_name): continue diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 2437845049..1cca7a8e84 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -354,11 +354,12 @@ Specifies the database to flush. Defaults to ``default``. ``inspectdb`` ------------- -.. django-admin:: inspectdb +.. django-admin:: inspectdb [table [table ...]] Introspects the database tables in the database pointed-to by the :setting:`NAME` setting and outputs a Django model module (a ``models.py`` -file) to standard output. +file) to standard output. You may choose what tables to inspect by passing +their names as arguments. 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 @@ -406,6 +407,11 @@ table's lifecycle, you'll need to change the :attr:`~django.db.models.Options.managed` option to ``True`` (or simply remove it because ``True`` is its default value). +.. versionadded:: 1.10 + + Support for the ``table`` argument(s) to choose what tables should be + inspected was added. + .. django-admin-option:: --database DATABASE Specifies the database to introspect. Defaults to ``default``. diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 4633901631..f612f22754 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -292,6 +292,9 @@ Management Commands * The :djadmin:`shell` command supports tab completion on systems using ``libedit``, e.g. Mac OSX. +* The :djadmin:`inspectdb` command lets you choose what tables should be + inspected by specifying their names as arguments. + Migrations ~~~~~~~~~~ diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index 6fd1778b55..37ba8db89c 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -28,6 +28,17 @@ class InspectDBTestCase(TestCase): # inspected self.assertNotIn("class DjangoContentType(models.Model):", out.getvalue(), msg=error_message) + def test_table_option(self): + """ + inspectdb can inspect a subset of tables by passing the table names as + arguments. + """ + out = StringIO() + call_command('inspectdb', 'inspectdb_people', stdout=out) + output = out.getvalue() + self.assertIn('class InspectdbPeople(models.Model):', output) + self.assertNotIn("InspectdbPeopledata", output) + def make_field_type_asserter(self): """Call inspectdb and return a function to validate a field type in its output""" out = StringIO()