Code

Ticket #12572: 12572_syncdb.diff

File 12572_syncdb.diff, 3.1 KB (added by lorochka85, 4 years ago)
Line 
1Index: django/core/management/commands/syncdb.py
2===================================================================
3--- django/core/management/commands/syncdb.py   (revision 12120)
4+++ django/core/management/commands/syncdb.py   (working copy)
5@@ -2,15 +2,16 @@
6 import sys
7 
8 from django.conf import settings
9-from django.core.management.base import NoArgsCommand
10+from django.core.management.base import BaseCommand, CommandError
11 from django.core.management.color import no_style
12 from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
13 from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
14 from django.utils.importlib import import_module
15+from django.core.exceptions import ImproperlyConfigured
16 
17 
18-class Command(NoArgsCommand):
19-    option_list = NoArgsCommand.option_list + (
20+class Command(BaseCommand):
21+    option_list = BaseCommand.option_list + (
22         make_option('--noinput', action='store_false', dest='interactive', default=True,
23             help='Tells Django to NOT prompt the user for input of any kind.'),
24         make_option('--database', action='store', dest='database',
25@@ -19,9 +20,11 @@
26         make_option('-e', '--exclude', dest='exclude',action='append', default=[],
27             help='App to exclude (use multiple --exclude to exclude multiple apps).'),
28     )
29-    help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
30+    help = ("Create the database tables for the apps in argument list or for all "
31+            "apps in INSTALLED_APPS if no app names given whose tables haven't already been created.")
32+    args = '[appname ...]'
33 
34-    def handle_noargs(self, **options):
35+    def handle(self, *app_labels, **options):
36 
37         verbosity = int(options.get('verbosity', 1))
38         interactive = options.get('interactive')
39@@ -32,7 +35,7 @@
40 
41         # Import the 'management' module within each installed app, to register
42         # dispatcher events.
43-        for app_name in settings.INSTALLED_APPS:
44+        for app_name in app_labels or settings.INSTALLED_APPS:
45             try:
46                 import_module('.management', app_name)
47             except ImportError, exc:
48@@ -59,9 +62,19 @@
49         created_models = set()
50         pending_references = {}
51 
52-        excluded_apps = set(models.get_app(app_label) for app_label in exclude)
53-        included_apps = set(app for app in models.get_apps() if app not in excluded_apps)
54+        try:
55+            excluded_apps = set(models.get_app(app_label) for app_label in exclude)
56+        except ImproperlyConfigured, e:
57+            raise CommandError(e)
58 
59+        if len(app_labels) == 0:
60+            included_apps = set(app for app in models.get_apps() if app not in excluded_apps)
61+        else:
62+            try:
63+                included_apps = set(models.get_app(app_label) for app_label in app_labels if not app_label in exclude)
64+            except ImproperlyConfigured, e:
65+                raise CommandError(e)
66+
67         # Create the tables for each model
68         for app in included_apps:
69             app_name = app.__name__.split('.')[-2]