Skip to content

Instantly share code, notes, and snippets.

@iarp
Last active August 26, 2019 16:12
Show Gist options
  • Save iarp/a41eb74f406f1918c97dc7e9aa9e2ea7 to your computer and use it in GitHub Desktop.
Save iarp/a41eb74f406f1918c97dc7e9aa9e2ea7 to your computer and use it in GitHub Desktop.
Include celery tasks in a reusable django app without knowing the project name.

Allows you to generate the app needed for celery tasks without knowing the main project name and celery config.

Otherwise you need to somehow know the project name to be able to from proj_name.celery import app

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings.SETTINGS_MODULE)
project = '.'.join(settings.SETTINGS_MODULE.split('.')[:-1])
app = Celery(project)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment