web/lib/django/core/management/commands/dbshell.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 from optparse import make_option
       
     2 
       
     3 from django.core.management.base import BaseCommand, CommandError
       
     4 from django.db import connections, DEFAULT_DB_ALIAS
       
     5 
       
     6 class Command(BaseCommand):
       
     7     help = ("Runs the command-line client for specified database, or the "
       
     8         "default database if none is provided.")
       
     9 
       
    10     option_list = BaseCommand.option_list + (
       
    11         make_option('--database', action='store', dest='database',
       
    12             default=DEFAULT_DB_ALIAS, help='Nominates a database onto which to '
       
    13                 'open a shell.  Defaults to the "default" database.'),
       
    14     )
       
    15 
       
    16     requires_model_validation = False
       
    17 
       
    18     def handle(self, **options):
       
    19         connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
       
    20         try:
       
    21             connection.client.runshell()
       
    22         except OSError:
       
    23             # Note that we're assuming OSError means that the client program
       
    24             # isn't installed. There's a possibility OSError would be raised
       
    25             # for some other reason, in which case this error message would be
       
    26             # inaccurate. Still, this message catches the common case.
       
    27             raise CommandError('You appear not to have the %r program installed or on your path.' % \
       
    28                 connection.client.executable_name)