Django Celery Results

2.6.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

To quickly integrate `django-celery-results`, first add `django_celery_results` to your `INSTALLED_APPS` in `settings.py`. Then, configure Celery to use the Django ORM backend by setting `CELERY_RESULT_BACKEND = 'django-db'` and run Django migrations. It's also recommended to set `CELERY_RESULT_EXTENDED = True` to store full task metadata like arguments and names. A basic Celery app and a sample task are shown, along with how to retrieve results from the task object or directly from the database model.

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

view raw JSON →