Django Celery Beat
django-celery-beat is an extension that enables storing and managing Celery periodic task schedules directly within the Django database. It provides a convenient Django Admin interface for creating, editing, and deleting these tasks, offering dynamic scheduling capabilities without code changes. The current version is 2.9.0, and the library maintains a regular release cadence with several updates annually.
Warnings
- breaking Always ensure your `django-celery-beat` version is compatible with your `Django` and `Celery` versions. Recent versions (2.8.0, 2.7.0, 2.6.0) have added explicit support for `Django` 5.0, 5.1, 5.2 and `Python` 3.12, 3.13. `celery>=5.0,<6.0` is generally required. Consult the official changelog for specific compatibility matrices to avoid unexpected behavior or errors.
- gotcha If you change your Django `TIME_ZONE` setting, existing periodic task schedules will still be based on the old timezone. To force Celery Beat to re-evaluate schedules with the new timezone, you must reset the `last_run_at` field for all periodic tasks.
- gotcha When configuring `CELERY_BEAT_SCHEDULER` in Django's `settings.py`, you must ensure your `celery.py` file uses `app.config_from_object('django.conf:settings', namespace='CELERY')`. Without `namespace='CELERY'`, Celery Beat may default to the `PersistentScheduler` (file-based) instead of `DatabaseScheduler`.
- gotcha You must ensure only a single Celery Beat scheduler process is running for a given schedule at any time. Running multiple Beat instances will lead to duplicate task executions.
- deprecated The `djcelery` library, which previously provided Django integration for Celery, is deprecated and does not support modern Django versions. Users migrating from older setups should switch to `django-celery-beat` for periodic tasks.
Install
-
pip install django-celery-beat
Imports
- PeriodicTask
from django_celery_beat.models import PeriodicTask
- IntervalSchedule
from django_celery_beat.models import IntervalSchedule
- CrontabSchedule
from django_celery_beat.models import CrontabSchedule
- SolarSchedule
from django_celery_beat.models import SolarSchedule
- ClockedSchedule
from django_celery_beat.models import ClockedSchedule
Quickstart
# myproject/settings.py
INSTALLED_APPS = [
# ...
'django.contrib.admin',
'django_celery_beat',
# 'myapp' where your tasks are defined
]
# Configure Celery Beat scheduler to use the database
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
# myproject/celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Auto-discover tasks in all installed apps
app.autodiscover_tasks()
@app.task(bind=True, ignore_result=True)
def debug_task(self):
print(f'Request: {self.request!r}')
# myapp/tasks.py
from celery import shared_task
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
@shared_task
def my_periodic_task():
logger.info('Executing my_periodic_task!')
# To run:
# 1. Apply migrations: python manage.py migrate django_celery_beat
# 2. Start Celery worker: celery -A myproject worker -l info
# 3. Start Celery Beat: celery -A myproject beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
# 4. In Django Admin (e.g., http://127.0.0.1:8000/admin/django_celery_beat/periodictask/add/):
# - Create a Crontab Schedule (e.g., 'Every minute': minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*')
# - Create a Periodic Task:
# - Name: 'Run My Periodic Task'
# - Task: 'myapp.tasks.my_periodic_task' (full path to your task)
# - Schedule: Choose the Crontab Schedule created above
# - Enabled: True