Getting started

To use https://pypi.org/project/django-celery-results/ with your project you need to follow these steps:

  1. Install the https://pypi.org/project/django-celery-results/ library:

    $ pip install django-celery-results
    
  2. Add django_celery_results to INSTALLED_APPS in your Django project’s settings.py:

    INSTALLED_APPS = (
        ...,
        'django_celery_results',
    )
    

    Note that there is no dash in the module name, only underscores.

  3. Create the Celery database tables by performing a database migrations:

    $ python manage.py migrate django_celery_results
    
  4. Configure Celery to use the https://pypi.org/project/django-celery-results/ backend.

    Assuming you are using Django’s settings.py to also configure Celery, add the following settings:

    CELERY_RESULT_BACKEND = 'django-db'
    

    For the cache backend you can use:

    CELERY_CACHE_BACKEND = 'django-cache'
    

    We can also use the cache defined in the CACHES setting in django.

    # celery setting.
    CELERY_CACHE_BACKEND = 'default'
    
    # django setting.
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
            'LOCATION': 'my_cache_table',
        }
    }
    

    If you want to include extended information about your tasks remember to enable the result_extended setting.

    CELERY_RESULT_EXTENDED = True
    

    If you want to track the execution duration of your tasks (by comparing date_started and date_done in TaskResult), enable the track_started setting.

    CELERY_TASK_TRACK_STARTED = True
    

    For example, if you write [additional codes](https://github.com/celery/django-celery-results/issues/286#issuecomment-1789094153) to record when a task becomes PENDING, you can calculate the waiting time in the queue or the actual processing time of the worker.

    task_result = TaskResult.objects.get(task_id='xxx')
    
    waiting_time = task_result.date_started - task_result.date_created
    processing_time = task_result.date_done - task_result.date_started
    total_time = task_result.date_done - task_result.date_created
    print(f'result: {waiting_time=}, {processing_time=}, {total_time=}')