Overview
Django 1.6, released on November 6, 2013, marks the transition to full Python 3 support. Where Django 1.5 offered experimental support, this release is fully compatible with Python 3.2 and 3.3. It also simplifies transaction management with autocommit by default and the atomic decorator.
Key Features
Simplified transaction management
Django 1.6 enables autocommit by default on databases, in line with standard SQL behavior. The atomic decorator and context manager replaces the older transaction decorators, providing a unified and intuitive API for wrapping code blocks in a transaction.
from django.db import transaction
# Usage as a decorator
@transaction.atomic
def transfer_funds(source_account, dest_account, amount):
"""Transfer funds between two accounts."""
source_account.balance -= amount
source_account.save()
dest_account.balance += amount
dest_account.save()
# Usage as a context manager
def register_participant(event, user):
"""Register a participant and update the counter."""
with transaction.atomic():
Registration.objects.create(
event=event,
participant=user,
)
event.registered_count += 1
event.save()
Connection pooling
The CONN_MAX_AGE setting allows reusing database connections across HTTP requests, avoiding the cost of opening and closing a connection on every request. This noticeably improves performance for high-traffic applications.
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_database',
'CONN_MAX_AGE': 600, # Connection reused for 10 minutes
}
}
# CONN_MAX_AGE = 0: close after each request (default)
# CONN_MAX_AGE = None: persistent connection indefinitely
# CONN_MAX_AGE = 600: keep the connection for 10 minutes
