Overview
Django 1.7, released on September 2, 2014, is a major release that integrates a schema migration system directly into the framework. Until now, developers had to use the third-party library South to manage database evolution. Django 1.7 makes this tool built-in with the makemigrations and migrate commands.
Key Features
Built-in migrations
The migration system automatically detects changes made to models and generates migration files. These files are version-controllable and allow reproducing the schema evolution on any environment.
# After modifying a model:
# python manage.py makemigrations
# Migrations for 'catalog':
# catalog/migrations/0002_product_weight.py
# - Add field weight to product
# Apply the migrations:
# python manage.py migrate
# Example of a generated migration file
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('catalog', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='product',
name='weight',
field=models.DecimalField(
max_digits=6, decimal_places=2, null=True
),
),
]
Application registry
The new application registry (AppConfig) provides a clean entry point for configuring each Django application. It allows defining human-readable names, running initialization code at startup, and connecting signals reliably.
# catalog/apps.py
from django.apps import AppConfig
class CatalogConfig(AppConfig):
name = 'catalog'
verbose_name = 'Product Catalog'
def ready(self):
"""Code executed when the application starts."""
import catalog.signals # noqa: F401
# catalog/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from catalog.models import Product
@receiver(post_save, sender=Product)
def notify_new_product(sender, instance, created, **kwargs):
if created:
print(f"New product added: {instance.name}")
