The Built-in Background Tasks Framework
Django 6.0 integrates a background tasks framework directly into the core. For simple use cases (sending emails, cleanup, notifications), you no longer need Celery or RQ: Django handles it natively using the database as a broker.
Configuration and Usage
The system uses the existing database as a queue. Define a task with the @task decorator and launch it with .enqueue(). A built-in worker consumes the tasks.
python
# settings.py
INSTALLED_APPS = [
...
'django.contrib.tasks',
]
# Then: python manage.py migrate
# Start the worker: python manage.py taskworker
python
# tasks.py
from django.contrib.tasks import task
from django.core.mail import send_mail
@task
def send_welcome_email(user_id: int):
"""Send a welcome email in the background."""
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(pk=user_id)
send_mail(
subject="Welcome!",
message=f"Hello {user.first_name}, welcome to our site.",
from_email="noreply@example.com",
recipient_list=[user.email],
)
# views.py
from .tasks import send_welcome_email
def signup(request):
user = create_user(request.POST)
# Task is enqueued, response is immediate
send_welcome_email.enqueue(user.id)
return redirect('home')
Celery vs Built-in Tasks
The built-in framework is designed for simple cases. Celery remains relevant for complex workflows, task routing, advanced retries, and high-performance brokers (Redis, RabbitMQ).
