Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown command in django core managment

I use Docker, Python and Django in my TDD project. When I run dcoker command on console:

docker-compose run app sh -c "python manage.py test"

The get error with message:

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'...

Source code of files

File 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)

File 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!'))

Why docker can't found my wait_for_db command file?

like image 523
Andreas Hunter Avatar asked Dec 18 '25 19:12

Andreas Hunter


2 Answers

In case anyone else is looking for another answer to a similar problem, make sure you added the related app to installed_apps. I had the same issue and after 30 minutes of renaming the files and trying to copy everything right, I realized that I have not yet added my app to the installed app. Therefore Django did not recognize the new management command.

like image 80
Mohamed El Firdoussi Avatar answered Dec 20 '25 11:12

Mohamed El Firdoussi


I think you got this error because the folder name in your app should be management, not managment ^_^

like image 37
Igor Belkov Avatar answered Dec 20 '25 11:12

Igor Belkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!