没有工作数据库连接的Django管理命令 [英] Django management command without working db connection

查看:112
本文介绍了没有工作数据库连接的Django管理命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多使用以下配置模型的项目:

I have a number of projects that use the following configuration model:

settings.py包括默认配置和配置规范,主要用于开发目的. 默认设置(包括数据库设置)可以被外部配置文件覆盖,外部配置文件通常由管理员针对其管理的各种环境定义.为了简化管理员,我编写了一个管理命令并单独打包,该命令添加了根据settings.py

settings.py includes default configuration and config specifications, mainly used for development purposes. The default settings (including db settings) can be overridden by external configuration files, usually defined by admins for the various environments they manage. In order to ease the admins, I have written a management command and packaged separately, which adds the option to create example configuration files based on the default configuration in settings.py

试图运行命令而无法成功进行数据库连接的情况会引发django.db.utils.OperationalError.

Trying to run the command without the possibility for successful db connection, however, raises django.db.utils.OperationalError.

如何使该命令在没有数据库连接的情况下工作,因为它不需要一个数据库连接,并且通常在需要该命令时,很可能数据库连接配置不正确.

How can I make the command work without db connection, as it does not need one and normally when the command is needed, most probably the db connection is not configured properly.

settings.DATABASES != {},因为有默认的数据库设置.

settings.DATABASES != {} as there are the default db settings.

Django 1.10.6,Python 3.5.3

推荐答案

执行requires_system_checks = False

class Command(BaseCommand):
    requires_system_checks = False
    ...
    def add_arguments(self, parser):
        #rest of code

更多信息信息

这篇关于没有工作数据库连接的Django管理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆