What Is New in Django 1.5
Django 1.5 introduces a major feature with configurable User models and brings significant updates to the Python compatibility matrix. Here's a quick summary of the key changes.
| Category | Key Changes |
|---|---|
| New Features | Configurable User model, Python 3 support, StreamingHttpResponse |
| Improvements | QuerySet date/time field filtering, Transaction management, Logging configuration |
| Backwards Incompatibilities | Changes to Context.has_key(), Email field max_length |
| Deprecated Features | django.utils.simplejson, django.utils.encoding.StrAndUnicode |
How does the new swappable User model work?
The most significant feature in Django 1.5 is the long-anticipated configurable User model. This allows you to substitute your own custom user model for Django's built-in User model.
You start by creating a model that inherits from AbstractBaseUser. This gives you the core authentication functionality while letting you define the fields you need. The USERNAME_FIELD setting specifies which field is used as the unique identifier.
In practice, this means you're no longer forced to have a username and email structure. You could use an email address as the primary identifier or add fields like a unique employee ID from the very first migration.
What Python versions are supported in this release?
Django 1.5 officially expands support to include Python 3, specifically versions 3.2 and 3.3. This is a huge milestone for the framework's ecosystem.
It maintains full compatibility with Python 2.6 and 2.7, making it a true bridge release for teams planning their migration. The codebase uses compatibility wrappers from the six library to handle differences between Python 2 and 3.
This dual support matters because it lets you start new projects on Python 3 or begin porting existing applications without waiting for a future Django version.
How has QuerySet filtering improved for dates?
QuerySet API gains new methods for precise date and datetime filtering: date, year, month, day, week_day, hour, minute, and second.
You can now write queries like Entry.objects.filter(pub_date__hour=18) to find all entries published at 6 PM. This is much cleaner than the old approach of using extra SQL snippets for these common operations.
These lookups work with the database's native date extraction functions, so they're efficient and consistent across different database backends.
What's new with transaction management?
Transaction handling gets simpler with the introduction of the TransactionMiddleware deprecation and a new context manager approach.
The preferred way now is to use the transaction.atomic decorator or context manager. This gives you explicit control over transaction blocks instead of relying on the implicit request/response transaction middleware.
For example, you can wrap a specific block of code: with transaction.atomic(): and everything in that block will execute within a single transaction. This is more Pythonic and easier to reason about.
Are there any caching improvements?
The cached template loader receives a significant optimization. Instead of storing the entire template source in the cache, it now only caches the compiled template.
This change reduces memory usage in your cache since compiled templates are generally smaller than the template source code. It also improves performance slightly by avoiding re-compilation of cached templates.
The improvement is most noticeable in production environments with large template bases and memory-bound caching systems like memcached.
FAQ
Should I use the new User model for existing projects?
It's complicated. While you can create a custom user model in existing projects, it requires careful data migration planning. For new projects, definitely start with a custom user model even if you just mirror the default fields initially.
Is Django 1.5 production-ready for Python 3?
Yes, the Python 3 support is solid for greenfield projects. For migrating large existing codebases, test thoroughly since some third-party packages might not have been ported to Python 3 yet at the time of this release.
What happened to the simplejson module?django.utils.simplejson is deprecated since Python 2.6+ includes a sufficiently good json module. You should update your imports to use Python's standard json module directly.
Why was the max_length of EmailField increased?
It was increased from 75 to 254 characters to comply with RFC 5321, which specifies the maximum length of an email address. This prevents validation errors for legitimate long email addresses.
What's the deal with Context.has_key() removal?Context.has_key() was removed to better align with dictionary-like objects in Python 3. You should use the in operator instead: if 'key' in context:.