# 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:

```bash
pip install django-celery-results
```

## 2. Django Configuration

Update your Django project's `settings.py` file.

### Add to Installed Apps

```python
# settings.py

INSTALLED_APPS = [
    # ...
    'django_celery_results',
]
```

### Configure Result Backend

Tell Celery to use the Django database backend.

```python
# 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.

```bash
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.

1.  Start your Celery worker: `celery -A myproject worker -l INFO`
2.  Run a task in the shell:
    ```python
    from myapp.tasks import add
    add.delay(4, 4)
    ```
3.  Go to the **Django Admin** panel (`/admin/`).
4.  Look for **Celery Results** > **Task results**. You should see your task execution there.

[[programming/python/celery]] [[programming/python/django]]