以词组为参数调用Django命令

时间:2019-03-02 12:39:23

标签: python django zappa django-commands

我正在尝试运行命令

zappa manage staging 'run_sql --sql \"show databases\"'

但是出现以下错误:

zappa manage staging 'run_sql --sql "show databases"'
[START] RequestId: dd895134-dc62-41c3-987b-3ea550af1129 Version: $LATEST
[DEBUG] 2019-03-02T10:51:18.213Z dd895134-dc62-41c3-987b-3ea550af1129 Zappa Event: {'manage': 'run_sql --sql show databases'}
Error: unrecognized arguments: databases: CommandError
Traceback (most recent call last):
  File "/var/task/handler.py", line 580, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 248, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 413, in handler
    management.call_command(*event['manage'].split(' '))
  File "/var/task/django/core/management/__init__.py", line 127, in call_command
    defaults = parser.parse_args(args=parse_args)
  File "/var/task/django/core/management/base.py", line 55, in parse_args
    return super().parse_args(args, namespace)
  File "/var/lang/lib/python3.6/argparse.py", line 1737, in parse_args
    self.error(msg % ' '.join(argv))
  File "/var/task/django/core/management/base.py", line 61, in error
    raise CommandError("Error: %s" % message)
django.core.management.base.CommandError: Error: unrecognized arguments: databases

我尝试了

之类的各种方法
zappa manage staging 'run_sql --sql \"show databases\"'
zappa manage staging 'run_sql --sql \\\"show databases\\\"'
zappa manage staging 'run_sql --sql \"show\ databases\"'
zappa manage staging 'run_sql --sql \\\"show\\\ databases\\\"'

但是由于相同的原因全部失败:

  

django.core.management.base.CommandError:错误:无法识别的参数:数据库\\”

貌似Django命令解析器相信该短语是几个选项或某些选项,但我不知道将其视为单个参数的方式。

基于以下Django命令:

import MySQLdb as db
from django.conf import settings
from django.core.management.base import BaseCommand


class Command(BaseCommand):
  help = "Runs a raw SQL query"

  def add_arguments(self, parser):
    parser.add_argument(
      '--user',
      dest='user',
      help='Username to be used to create database',
    )
    parser.add_argument(
      '--password',
      dest='password',
      help='Password for the mysql user.',
    )
    parser.add_argument(
      '--db-name',
      dest='db_name',
      help='Name of the database if it is different from the one defined in settings',
    )
    parser.add_argument(
      '--sql',
      dest='sql',
      help='SQL query to run',
      default='show databases'
    )

  def handle(self, *args, **options):
    self.stdout.write(self.style.SUCCESS('Running raw SQL query'))

    sql = options.get('sql')
    dbname = options.get('db_name') or settings.DATABASES['default']['NAME']
    user = options.get('user') or settings.DATABASES['default']['USER']
    password = options.get('password') or settings.DATABASES['default']['PASSWORD']
    host = settings.DATABASES['default']['HOST']

    self.stdout.write(self.style.SUCCESS(
      'Running SQL query\n'
      '"{sql}"\n'
      'with \n'
      'dbname="{dbname}"\n'
      'user="{user}"\n'
      'password="{password}"\n'
      'host="{host}"\n'
      ''.format(
        sql=sql,
        dbname=dbname,
        user=user,
        password=password,
        host=host,
      )
    ))

    con = db.connect(user=user, host=host, password=password)
    cur = con.cursor()
    cur.execute(f'{sql}'.format(sql=sql))

    output = cur.fetchall()
    self.stdout.write(self.style.SUCCESS(
      "\nSQL query output:\n"
      "{output}".format(output=output))
    )
    self.stdout.write(self.style.SUCCESS('\n'))

    cur.close()
    con.close()

    self.stdout.write(self.style.SUCCESS('All Done'))

0 个答案:

没有答案