Django Celery Results
Django Celery Results is an extension that enables you to store Celery task and group results using the Django ORM or Django's cache framework. It provides database models (TaskResult and GroupResult) to store task and group outcomes, which can then be queried like any other Django model. The library is actively maintained, with new versions typically released every few months to support the latest Django and Celery versions.
Warnings
- breaking As of v2.4.0, the `TaskResult.task_name` field is no longer automatically populated by default due to a security concern. If your application relies on this field, you must explicitly set `CELERY_RESULT_EXTENDED = True` in your Django settings to store extended properties, which includes the task name, args, and kwargs.
- breaking Support for older Django and Python versions has been dropped. v2.4.0 dropped Django 2.2 support, and v2.0.0 dropped support for Django < 2.2 and Python < 3.6. Ensure your environment meets the minimum requirements.
- gotcha When using MySQL, you might encounter `django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')` during migrations. This is due to indexing long string fields.
- deprecated The `default_app_config` attribute was deprecated in Django 3.1 and removed in Django 4.0. `django-celery-results` fixed this in v2.3.0. If you are on an older version of `django-celery-results` with Django 3.1+, you might see deprecation warnings.
- gotcha A common pitfall in Django and Celery integration is dispatching a task immediately that relies on database changes that haven't been committed yet. The Celery task might run before the database transaction is complete.
- gotcha The default Celery configuration might not store detailed task arguments, keyword arguments, or other metadata. This can make debugging or re-running tasks difficult.
Install
-
pip install django-celery-results
Imports
- django_celery_results
INSTALLED_APPS = ['django_celery_results']
- TaskResult
from django_celery_results.models import TaskResult
- GroupResult
from django_celery_results.models import GroupResult
Quickstart
# myproject/settings.py
INSTALLED_APPS = [
# ... other apps
'django_celery_results',
]
CELERY_RESULT_BACKEND = 'django-db'
CELERY_RESULT_EXTENDED = True # Recommended to store task args/kwargs/name
# myproject/celery.py (or similar Celery app configuration)
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
# myapp/tasks.py
from celery import shared_task
@shared_task
def add(x, y):
return x + y
# To run and get result in Django shell:
# from myapp.tasks import add
# result = add.delay(4, 5)
# print(result.get(timeout=10)) # Output: 9
# Or to query from the DB:
# from django_celery_results.models import TaskResult
# task_db_result = TaskResult.objects.get(task_id=result.id)
# print(task_db_result.status)
# print(task_db_result.result)