Overview
Django 1.1, released on July 29, 2009, enriches the framework with ORM aggregation features and faster transaction-based tests. This release consolidates the foundations laid by Django 1.0 by making database queries more expressive.
Key Features
ORM aggregation
Django 1.1 introduces support for aggregation functions (Count, Sum, Avg, Max, Min) directly in the ORM, enabling complex calculations without raw SQL.
python
from django.db.models import Count, Avg, Sum
# Number of articles per author
active_authors = (
Author.objects
.annotate(num_articles=Count('article'))
.filter(num_articles__gt=0)
.order_by('-num_articles')
)
for author in active_authors:
print(f"{author.name}: {author.num_articles} articles")
# Global statistics
stats = Article.objects.aggregate(
total=Count('id'),
avg_length=Avg('content__length'),
)
print(f"Total: {stats['total']}, Average length: {stats['avg_length']:.0f}")
Transaction-based tests
The TransactionTestCase class provides fine-grained control over transactions in tests. Combined with TestCase, which uses transactions to speed up tests, Django 1.1 makes the test suite significantly faster.
python
from django.test import TestCase, TransactionTestCase
class ArticleTestCase(TestCase):
"""Tests in TestCase are wrapped in a transaction.
The database is automatically restored after each test."""
def setUp(self):
self.author = Author.objects.create(
name="Marie Dupont",
email="marie@example.com",
)
def test_create_article(self):
article = Article.objects.create(
title="My article",
content="The content.",
author=self.author,
)
self.assertEqual(article.author.name, "Marie Dupont")
self.assertFalse(article.published)
