You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbshell.py 1.2KB

12345678910111213141516171819202122232425262728293031
  1. from django.core.management.base import BaseCommand, CommandError
  2. from django.db import DEFAULT_DB_ALIAS, connections
  3. class Command(BaseCommand):
  4. help = (
  5. "Runs the command-line client for specified database, or the "
  6. "default database if none is provided."
  7. )
  8. requires_system_checks = False
  9. def add_arguments(self, parser):
  10. parser.add_argument(
  11. '--database', default=DEFAULT_DB_ALIAS,
  12. help='Nominates a database onto which to open a shell. Defaults to the "default" database.',
  13. )
  14. def handle(self, **options):
  15. connection = connections[options['database']]
  16. try:
  17. connection.client.runshell()
  18. except OSError:
  19. # Note that we're assuming OSError means that the client program
  20. # isn't installed. There's a possibility OSError would be raised
  21. # for some other reason, in which case this error message would be
  22. # inaccurate. Still, this message catches the common case.
  23. raise CommandError(
  24. 'You appear not to have the %r program installed or on your path.' %
  25. connection.client.executable_name
  26. )