Ticket #12572: 12572_syncdb.diff

File 12572_syncdb.diff, 3.1 KB (added by lorochka85, 15 years ago)
  • django/core/management/commands/syncdb.py

     
    22import sys
    33
    44from django.conf import settings
    5 from django.core.management.base import NoArgsCommand
     5from django.core.management.base import BaseCommand, CommandError
    66from django.core.management.color import no_style
    77from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
    88from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
    99from django.utils.importlib import import_module
     10from django.core.exceptions import ImproperlyConfigured
    1011
    1112
    12 class Command(NoArgsCommand):
    13     option_list = NoArgsCommand.option_list + (
     13class Command(BaseCommand):
     14    option_list = BaseCommand.option_list + (
    1415        make_option('--noinput', action='store_false', dest='interactive', default=True,
    1516            help='Tells Django to NOT prompt the user for input of any kind.'),
    1617        make_option('--database', action='store', dest='database',
     
    1920        make_option('-e', '--exclude', dest='exclude',action='append', default=[],
    2021            help='App to exclude (use multiple --exclude to exclude multiple apps).'),
    2122    )
    22     help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
     23    help = ("Create the database tables for the apps in argument list or for all "
     24            "apps in INSTALLED_APPS if no app names given whose tables haven't already been created.")
     25    args = '[appname ...]'
    2326
    24     def handle_noargs(self, **options):
     27    def handle(self, *app_labels, **options):
    2528
    2629        verbosity = int(options.get('verbosity', 1))
    2730        interactive = options.get('interactive')
     
    3235
    3336        # Import the 'management' module within each installed app, to register
    3437        # dispatcher events.
    35         for app_name in settings.INSTALLED_APPS:
     38        for app_name in app_labels or settings.INSTALLED_APPS:
    3639            try:
    3740                import_module('.management', app_name)
    3841            except ImportError, exc:
     
    5962        created_models = set()
    6063        pending_references = {}
    6164
    62         excluded_apps = set(models.get_app(app_label) for app_label in exclude)
    63         included_apps = set(app for app in models.get_apps() if app not in excluded_apps)
     65        try:
     66            excluded_apps = set(models.get_app(app_label) for app_label in exclude)
     67        except ImproperlyConfigured, e:
     68            raise CommandError(e)
    6469
     70        if len(app_labels) == 0:
     71            included_apps = set(app for app in models.get_apps() if app not in excluded_apps)
     72        else:
     73            try:
     74                included_apps = set(models.get_app(app_label) for app_label in app_labels if not app_label in exclude)
     75            except ImproperlyConfigured, e:
     76                raise CommandError(e)
     77
    6578        # Create the tables for each model
    6679        for app in included_apps:
    6780            app_name = app.__name__.split('.')[-2]
Back to Top