CommandError: Unknown command:
I created a custom command and want to run it automatically on start
My command file
# project/parser_app/management/commands/create_schedules.py
class Command(loaddata.Command):
def handle(self, *args, **options):
# some code
Added command to wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from django.core.management import call_command
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core_app.settings')
application = get_wsgi_application()
call_command("makemigrations")
call_command("migrate")
call_command("collectstatic", interactive=False)
call_command("create_schedules parser_app/management/commands/schedule_fixtures.json")
I can run in console command python manage.py "create_schedules parser_app/management/commands/schedule_fixtures.json"
and everything is working but when I added command to wsgi.py (to run on start)
import os
from django.core.wsgi import get_wsgi_application
from django.core.management import call_command
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core_app.settings')
application = get_wsgi_application()
call_command("makemigrations")
call_command("migrate")
call_command("collectstatic", interactive=False)
call_command("create_schedules parser_app/management/commands/schedule_fixtures.json")
I have an error
raise CommandError("Unknown command: %r" % command_name) django_1 | django.core.management.base.CommandError: Unknown command: 'create_schedules parser_app/management/commands/schedule_fixtures.json'
UPDATE
I was trying to run command as a function^ but have an error
ile "/project/core_app/wsgi.py", line 14, in <module>
django_1 | from parser_app.management.commands.start_command import create_schedules
django_1 | File "/project/parser_app/management/commands/start_command.py", line 1, in <module>
django_1 | from parser_app.models import Schedule
django_1 | File "/project/parser_app/models.py", line 7, in <module>
django_1 | class TimeBase(models.Model):
django_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 127, in __new__
django_1 | app_config = apps.get_containing_app_config(module)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
django_1 | self.check_apps_ready()
django_1 | File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 138, in check_apps_ready
Incorrect syntax, see below for correction:
call_command("create_schedules", "parser_app/management/commands/schedule_fixtures.json")
Simpler still would be to not have the code as a management command:
# create_schedules.py
def create_schedules(file_path):
...
# wsgi.py
import create_schedules as cs
[...]
cs.create_schedules("parser_app/management/commands/schedule_fixtures.json")
Solved with help of ready() function in apps.py
My solution
class ParserAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'parser_app'
def ready(self):
from scripts.create_schedules import create_cron_templates
create_cron_templates()