Storing Celery Results in Django Database
By default, Celery might use Redis or RabbitMQ (RPC) to store results. However, if you are using Django, it is often convenient to store task results directly in your SQL database using the django-celery-results extension. This allows you to view task status and results via the Django Admin interface.
1. Installation
Install the library using pip:
pip install django-celery-results
2. Django Configuration
Update your Django project's settings.py file.
Add to Installed Apps
# settings.py
INSTALLED_APPS = [
# ...
'django_celery_results',
]
Configure Result Backend
Tell Celery to use the Django database backend.
# settings.py
CELERY_RESULT_BACKEND = 'django-db'
# Optional: Cache results for performance (requires a cache backend to be configured)
# CELERY_CACHE_BACKEND = 'default'
3. Apply Migrations
Since this extension creates new database tables to store the results, you need to run migrations.
python manage.py migrate django_celery_results
This creates tables like django_celery_results_taskresult.
4. Verification
Now, when you run a task, the result will be saved to the database.
- Start your Celery worker:
celery -A myproject worker -l INFO - Run a task in the shell:
from myapp.tasks import add add.delay(4, 4) - Go to the Django Admin panel (
/admin/). - Look for Celery Results > Task results. You should see your task execution there.