python - django核心管理中的未知命令
问题描述
我在我的 TDD 项目中使用 Docker、Python 和 Django。当我在控制台上运行 dcoker 命令时:
docker-compose run app sh -c "python manage.py test"
带有消息的获取错误:
Starting recipe-app-api_db_1 ... done
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...EE....
======================================================================
ERROR: test_wait_for_db (core.tests.test_commands.CommandsTestCase)
Test waiting for db
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 103, in call_command
app_name = get_commands()[command_name]
KeyError: 'wait_for_db'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/unittest/mock.py", line 1348, in patched
return func(*newargs, **newkeywargs)
File "/app/core/tests/test_commands.py", line 24, in test_wait_for_db
call_command('wait_for_db')
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 105, in call_command
raise CommandError("Unknown command: %r" % command_name)
django.core.management.base.CommandError: Unknown command: 'wait_for_db'
======================================================================
ERROR: test_wait_for_db_ready (core.tests.test_commands.CommandsTestCase)
Test waiting for db when db is available
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 103, in call_command
app_name = get_commands()[command_name]
KeyError: 'wait_for_db'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/core/tests/test_commands.py", line 15, in test_wait_for_db_ready
call_command('wait_for_db')
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 105, in call_command
raise CommandError("Unknown command: %r" % command_name)
django.core.management.base.CommandError: Unknown command: 'wait_for_db'
----------------------------------------------------------------------
Ran 9 tests in 5.529s
FAILED (errors=2)
Destroying test database for alias 'default'...
文件源代码
文件app/tests/test_commands.py
:
from unittest.mock import patch
from django.core.management import call_command
from django.db.utils import OperationalError
from django.test import TestCase
class CommandsTestCase(TestCase):
def test_wait_for_db_ready(self):
"""Test waiting for db when db is available"""
with patch('django.db.utils.ConnectionHandler.__getitem__') as gi:
gi.return_value = True
call_command('wait_for_db')
self.assertEqual(gi.call_count, 1)
@patch('time.sleep', return_value=None)
def test_wait_for_db(self, ts):
"""Test waiting for db"""
with patch('django.db.utils.ConnectionHandler.__getitem__') as gi:
gi.side_effect = [OperationalError] * 5 + [True]
call_command('wait_for_db')
self.assertEqual(gi.call_count, 6)
文件app/core/managment/commands/wait_for_db.py
:
import time
from django.db import connection
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""Django command that waits for database to be available"""
def handle(self, *args, **options):
"""Handle the command"""
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
connection.ensure_connection()
db_conn = True
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
为什么docker找不到我的wait_for_db
命令文件?
解决方案
我认为您收到此错误是因为您的应用程序中的文件夹名称应该是management
,而不是managment
^_^
推荐阅读
- javascript - html 属性不适用于 js 和 jquery
- php - 将php连接到ms sql server数据库
- mongodb - 如何将 MongoDb 查询转换为 csv(excel 格式)
- java - java - 如何在java中的arraylist中获取随时间升序的日期
- python - 检查 discord 命令参数
- angular - 如何在 Angular2 中使用 plyr npm 模块在 Angular2 中创建视频播放器?
- sql - 批量插入多个选项卡 - 从多个表导出
- javascript - 使用 jquery 创建选中的复选框数组
- whatsapp - IBM Watson 与 WhatsApp 的集成
- php - 为什么当将值作为参数时,file_exists 总是返回 false?