2đź‘Ť
Applications can register their own actions with manage.py
. For example, you might want to add a manage.py
action for a Django app that you’re distributing.
To do this, add a management/commands directory to the application. Django will register a manage.py
command for each Python module in that directory whose name doesn’t begin with an underscore. For example:
YOUR_APP/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
runcelery.py
tests.py
views.py
In this example, the runcelery command will be made available to any project that includes the YOUR_APP application in INSTALLED_APPS.
The runcelery.py
module has only one requirement – it must define a class Command that extends BaseCommand or one of its subclasses.
To implement the command, edit YOUR_APP/management/commands/runcelery.py
to look like this:
from django.core.management.base import BaseCommand, CommandError
from celery.bin import worker
class Command(BaseCommand):
help = 'Run celery worker'
def handle(self, *args, **options):
worker.worker(app=my_app).run()
you can now run it from manage.py like this: python manage.py runcelery
check this article for more information.