Welcome to Django 1.7!
These release notes cover the new features, as well as some backwards incompatible changes you’ll want to be aware of when upgrading from Django 1.6 or older versions. We’ve also dropped some features, which are detailed in our deprecation plan, and we’ve begun the deprecation process for some features.
Django 1.7 requires Python 2.7 or above, though we highly recommend the latest minor release. Support for Python 2.6 has been dropped and support for Python 3.4 has been added.
This change should affect only a small number of Django users, as most operating-system vendors today are shipping Python 2.7 or newer as their default version. If you’re still using Python 2.6, however, you’ll need to stick to Django 1.6 until you can upgrade your Python version. Per our support policy, Django 1.6 will continue to receive security support until the release of Django 1.8.
Django now has built-in support for schema migrations. It allows models to be updated, changed, and deleted by creating migration files that represent the model changes and which can be run on any development, staging or production database.
Migrations are covered in their own documentation, but a few of the key features are:
syncdb has been deprecated and replaced by migrate. Don’t worry - calls to syncdb will still work as before.
A new makemigrations command provides an easy way to autodetect changes to your models and make migrations for them.
pre_syncdb and post_syncdb have been replaced by pre_migrate and post_migrate respectively. These new signals have slightly different arguments. Check the documentation for details.
The allow_syncdb method on database routers is now called allow_migrate, but still performs the same function. Routers with allow_syncdb methods will still work, but that method name is deprecated and you should change it as soon as possible (nothing more than renaming is required).
initial_data fixtures are no longer loaded for apps with migrations; if you want to load initial data for an app, we suggest you do it in a migration.
Historically, Django applications were tightly linked to models. A singleton known as the “app cache” dealt with both installed applications and models. The models module was used as an identifier for applications in many APIs.
As the concept of Django applications matured, this code showed some shortcomings. It has been refactored into an “app registry” where models modules no longer have a central role and where it’s possible to attach configuration data to applications.
Improvements thus far include:
To help power both schema migrations and composite keys, the Field API now has a new required method: deconstruct().
This method takes no arguments, and returns a tuple of four items:
These four values allow any field to be serialized into a file, as well as allowing the field to be copied safely, both essential parts of these new features.
This change should not affect you unless you write custom Field subclasses; if you do, you may need to reimplement the deconstruct() method if your subclass changes the method signature of __init__ in any way. If your field just inherits from a built-in Django field and doesn’t override __init__, no changes are necessary.
If you do need to override deconstruct(), a good place to start is the built-in Django fields (django/db/models/fields/__init__.py) as several fields, including DecimalField and DateField, override it and show how to call the method on the superclass and simply add or remove extra arguments.
Historically, the recommended way to make reusable model queries was to create methods on a custom Manager class. The problem with this approach was that after the first method call, you’d get back a QuerySet instance and couldn’t call additional custom manager methods.
Though not documented, it was common to work around this issue by creating a custom QuerySet so that custom methods could be chained; but the solution had a number of drawbacks:
The QuerySet.as_manager() class method can now directly create Manager with QuerySet methods:
class FoodQuerySet(models.QuerySet):
def pizzas(self):
return self.filter(kind='pizza')
def vegetarian(self):
return self.filter(vegetarian=True)
class Food(models.Model):
kind = models.CharField(max_length=50)
vegetarian = models.BooleanField()
objects = FoodQuerySet.as_manager()
Food.objects.pizzas().vegetarian()
It is now possible to specify a custom manager when traversing a reverse relationship:
class Blog(models.Model):
pass
class Entry(models.Model):
blog = models.ForeignKey(Blog)
objects = models.Manager() # Default Manager
entries = EntryManager() # Custom Manager
b = Blog.objects.get(id=1)
b.entry_set(manager='entries').all()
We’ve added a new System check framework for detecting common problems (like invalid models) and providing hints for resolving those problems. The framework is extensible so you can add your own checks for your own apps and libraries.
To perform system checks, you use the check management command. This command replaces the older validate management command.
The “today” and “now” shortcuts next to date and time input widgets in the admin are now operating in the current time zone. Previously, they used the browser time zone, which could result in saving the wrong value when it didn’t match the current time zone on the server.
In addition, the widgets now display a help message when the browser and server time zone are different, to clarify how the value inserted in the field will be interpreted.
Prior to Python 2.7, database cursors could be used as a context manager. The specific backend’s cursor defined the behavior of the context manager. The behavior of magic method lookups was changed with Python 2.7 and cursors were no longer usable as context managers.
Django 1.7 allows a cursor to be used as a context manager that is a shortcut for the following, instead of backend specific behavior.
c = connection.cursor()
try:
c.execute(...)
finally:
c.close()
It is now possible to write custom lookups and transforms for the ORM. Custom lookups work just like Django’s inbuilt lookups (e.g. lte, icontains) while transforms are a new concept.
The django.db.models.Lookup class provides a way to add lookup operators for model fields. As an example it is possible to add day_lte operator for DateFields.
The django.db.models.Transform class allows transformations of database values prior to the final lookup. For example it is possible to write a year transform that extracts year from the field’s value. Transforms allow for chaining. After the year transform has been added to DateField it is possible to filter on the transformed value, for example qs.filter(author__birthdate__year__lte=1981).
For more information about both custom lookups and transforms refer to custom lookups documentation.
The static files storage classes may be subclassed to override the permissions that collected static files and directories receive by setting the file_permissions_mode and directory_permissions_mode parameters. See collectstatic for example usage.
The CachedStaticFilesStorage backend gets a sibling class called ManifestStaticFilesStorage that doesn’t use the cache system at all but instead a JSON file called staticfiles.json for storing the mapping between the original file name (e.g. css/styles.css) and the hashed file name (e.g. css/styles.55e7cbb9ba48.css. The staticfiles.json file is created when running the collectstatic management command and should be a less expensive alternative for remote storages such as Amazon S3.
See the ManifestStaticFilesStorage docs for more information.
findstatic now accepts verbosity flag level 2, meaning it will show the relative paths of the directories it searched. See findstatic for example output.
Warning
In addition to the changes outlined in this section, be sure to review the deprecation plan for any features that have been removed. If you haven’t updated your code within the deprecation timeline for a given feature, its removal may appear as a backwards incompatible change.
While Django will still look at allow_syncdb methods even though they should be renamed to allow_migrate, there is a subtle difference in which models get passed to these methods.
For apps with migrations, allow_migrate will now get passed historical models, which are special versioned models without custom attributes, methods or managers. Make sure your allow_migrate methods are only referring to fields or other items in model._meta.
Apps with migrations will not load initial_data fixtures when they have finished migrating. Apps without migrations will continue to load these fixtures during the phase of migrate which emulates the old syncdb behavior, but any new apps will not have this support.
Instead, you are encouraged to load initial data in migrations if you need it (using the RunPython operation and your model classes); this has the added advantage that your initial data will not need updating every time you change the schema.
Django 1.7 loads application configurations and models as soon as it starts. While this behavior is more straightforward and is believed to be more robust, regressions cannot be ruled out. You may encounter the following exceptions:
RuntimeError: App registry isn't ready yet. This happens when importing an application configuration or a models module triggers code that depends on the app registry.
For example, ugettext() uses the app registry to look up translation catalogs in applications. To translate at import time, you need ugettext_lazy() instead. (Using ugettext() would be a bug, because the translation would happen at import time, rather than at each request depending on the active language.)
Executing database queries with the ORM at import time in models modules will also trigger this exception. The ORM cannot function properly until all models are available.
Another common culprit is django.contrib.auth.get_user_model(). Use the AUTH_USER_MODEL setting to reference the User model at import time.
ImportError: cannot import name ... This happens if the import sequence ends up in a loop.
To eliminate such problems, you should minimize dependencies between your models modules and do as little work as possible at import time. To avoid executing code at import time, you can move it into a function and cache its results. The code will be executed when you first need its results. This concept is known as “lazy evaluation”.
django.contrib.admin will now automatically perform autodiscovery of admin modules in installed applications. To prevent it, change your INSTALLED_APPS to contain 'django.contrib.admin.apps.SimpleAdminConfig' instead of 'django.contrib.admin'.
If you’re using Django in a plain Python script — rather than a management command — and you rely on the DJANGO_SETTINGS_MODULE environment variable, you must now explicitly initialize Django at the beginning of your script with:
>>> import django
>>> django.setup()
Otherwise, you will hit RuntimeError: App registry isn't ready yet.
It is no longer possible to have multiple installed applications with the same label. In previous versions of Django, this didn’t always work correctly, but didn’t crash outright either.
If you have two apps with the same label, you should create an AppConfig for one of them and override its label there. You should then adjust your code wherever it references this application or its models with the old label.
It isn’t possible to import the same model twice through different paths any more. As of Django 1.6, this may happen only if you’re manually putting a directory and a subdirectory on PYTHONPATH. Refer to the section on the new project layout in the 1.4 release notes for migration instructions.
You should make sure that:
Django will enforce these requirements as of version 1.9, after a deprecation period.
Subclasses of AppCommand must now implement a handle_app_config() method instead of handle_app(). This method receives an AppConfig instance instead of a models module.
Since INSTALLED_APPS now supports application configuration classes in addition to application modules, you should review code that accesses this setting directly and use the app registry (django.apps.apps) instead.
The app registry has preserved some features of the old app cache. Even though the app cache was a private API, obsolete methods and arguments will be removed through a standard deprecation path, with the exception of the following changes that take effect immediately:
When several applications provide management commands with the same name, Django loads the command from the application that comes first in INSTALLED_APPS. Previous versions loaded the command from the application that came last.
This brings discovery of management commands in line with other parts of Django that rely on the order of INSTALLED_APPS, such as static files, templates, and translations.
An inconsistency existed in previous versions of Django regarding how pickle errors are handled by different cache backends. django.core.cache.backends.locmem.LocMemCache used to fail silently when such an error occurs, which is inconsistent with other backends and leads to cache-specific errors. This has been fixed in Django 1.7, see Ticket #21200 for more details.
Previous versions of Django generated cache keys using a request’s path and query string but not the scheme or host. If a Django application was serving multiple subdomains or domains, cache keys could collide. In Django 1.7, cache keys vary by the absolute URL of the request including scheme, host, path, and query string. For example, the URL portion of a cache key is now generated from http://www.example.com/path/to/?key=val rather than /path/to/?key=val. The cache keys generated by Django 1.7 will be different from the keys generated by older versions of Django. After upgrading to Django 1.7, the first request to any previously cached URL will be a cache miss .
In previous versions of Django, it was possible to use db_manager(using=None) on a model manager instance to obtain a manager instance using default routing behavior, overriding any manually specified database routing. In Django 1.7, a value of None passed to db_manager will produce a router that retains any manually assigned database routing – the manager will not be reset. This was necessary to resolve an inconsistency in the way routing information cascaded over joins. See Ticket #13724 for more details.
If your project handles datetimes before 1970 or after 2037 and Django raises a ValueError when encountering them, you will have to install pytz. You may be affected by this problem if you use Django’s time zone-related date formats or django.contrib.syndication.
Historically, the Django admin site passed the request from an unauthorized or unauthenticated user directly to the login view, without HTTP redirection. In Django 1.7, this behavior changed to conform to a more traditional workflow where any unauthorized request to an admin page will be redirected (by HTTP status code 302) to the login page, with the next parameter set to the referring path. The user will be redirected there after a successful login.
Note also that the admin login form has been updated to not contain the this_is_the_login_form field (now unused) and the ValidationError code has been set to the more regular invalid_login key.
The django.core.files.uploadhandler.FileUploadHandler.new_file() method is now passed an additional content_type_extra parameter. If you have a custom FileUploadHandler that implements new_file(), be sure it accepts this new parameter.
ModelFormSets no longer delete instances when save(commit=False) is called. See can_delete for instructions on how to manually delete objects from deleted forms.
Loading empty fixtures emits a RuntimeWarning rather than raising CommandError.
django.contrib.staticfiles.views.serve() will now raise an Http404 exception instead of ImproperlyConfigured when DEBUG is False. This change removes the need to conditionally add the view to your root URLconf, which in turn makes it safe to reverse by name. It also removes the ability for visitors to generate spurious HTTP 500 errors by requesting static files that don’t exist or haven’t been collected yet.
The django.db.models.Model.__eq__() method is now defined in a way where instances of a proxy model and its base model are considered equal when primary keys match. Previously only instances of exact same class were considered equal on primary key match.
The django.db.models.Model.__eq__() method has changed such that two Model instances without primary key values won’t be considered equal (unless they are the same instance).
The django.db.models.Model.__hash__() will now raise TypeError when called on an instance without a primary key value. This is done to avoid mutable __hash__ values in containers.
AutoField columns in SQLite databases will now be created using the AUTOINCREMENT option, which guarantees monotonic increments. This will cause primary key numbering behavior to change on SQLite, becoming consistent with most other SQL databases. This will only apply to newly created tables. If you have a database created with an older version of Django, you will need to migrate it to take advantage of this feature. For example, you could do the following:
django.contrib.auth.models.AbstractUser no longer defines a get_absolute_url() method. The old definition returned "/users/%s/" % urlquote(self.username) which was arbitrary since applications may or may not define such a url in urlpatterns. Define a get_absolute_url() method on your own custom user object or use ABSOLUTE_URL_OVERRIDES if you want a URL for your user.
The static asset-serving functionality of the django.test.LiveServerTestCase class has been simplified: Now it’s only able to serve content already present in STATIC_ROOT when tests are run. The ability to transparently serve all the static assets (similarly to what one gets with DEBUG = True at development-time) has been moved to a new class that lives in the staticfiles application (the one actually in charge of such feature): django.contrib.staticfiles.testing.StaticLiveServerCase. In other words, LiveServerTestCase itself is less powerful but at the same time has less magic.
Rationale behind this is removal of dependency of non-contrib code on contrib applications.
The old cache URI syntax (e.g. "locmem://") is no longer supported. It still worked, even though it was not documented or officially supported. If you’re still using it, please update to the current CACHES syntax.
The default ordering of Form fields in case of inheritance has changed to follow normal Python MRO. Fields are now discovered by iterating through the MRO in reverse with the topmost class coming last. This only affects you if you relied on the default field ordering while having fields defined on both the current class and on a parent Form.
The required argument of SelectDateWidget has been removed. This widget now respects the form field’s is_required attribute like other widgets.
Widget.is_hidden is now a read-only property, getting its value by introspecting the presence of input_type == 'hidden'.
select_related() now chains in the same way as other similar calls like prefetch_related. That is, select_related('foo', 'bar') is equivalent to select_related('foo').select_related('bar'). Previously the latter would have been equivalent to select_related('bar').
GeoDjango dropped support for GEOS < 3.1.
The init_connection_state method of database backends now executes in autocommit mode (unless you set AUTOCOMMIT to False). If you maintain a custom database backend, you should check that method.
The django.db.backends.BaseDatabaseFeatures.allows_primary_key_0 attribute has been renamed to allows_auto_pk_0 to better describe it. It’s True for all database backends included with Django except MySQL which does allow primary keys with value 0. It only forbids autoincrement primary keys with value 0.
Shadowing model fields defined in a parent model has been forbidden as this creates ambiguity in the expected model behavior. In addition, any clashing fields in the model inheritance hierarchy results in a system check error. For example, if you use multi-inheritance, you need to define custom primary key fields on parent models, otherwise the default id fields will clash.
django.utils.translation.parse_accept_lang_header() now returns lowercase locales, instead of the case as it was provided. As locales should be treated case-insensitive this allows us to speed up locale detection.
django.utils.translation.get_language_from_path() and django.utils.translation.trans_real.get_supported_language_variant() now no longer have a supported argument.
The shortcut view in django.contrib.contenttypes.views now supports protocol-relative URLs (e.g. //example.com).
GenericRelation now supports an optional related_query_name argument. Setting related_query_name adds a relation from the related object back to the content type for filtering, ordering and other query operations.
When a model field’s validators contains a RegexValidator, the regular expression must now be passed as a regular expression string. You can no longer use a pre-compiled regular expression in this case, as it is not serializable. The flags attribute was added to RegexValidator to simplify this change.
django.core.cache.get_cache() has been supplanted by django.core.cache.caches.
django.utils.dictconfig and django.utils.importlib were copies of respectively logging.config and importlib provided for Python versions prior to 2.7. They have been deprecated.
The current import_by_path() function catches AttributeError, ImportError and ValueError exceptions, and re-raises ImproperlyConfigured. Such exception masking makes it needlessly hard to diagnose circular import problems, because it makes it look like the problem comes from inside Django. It has been deprecated in favor of import_string().
django.utils.tzinfo provided two tzinfo subclasses, LocalTimezone and FixedOffset. They’ve been deprecated in favor of more correct alternatives provided by django.utils.timezone, django.utils.timezone.get_default_timezone() and django.utils.timezone.get_fixed_timezone().
django.utils.unittest provided uniform access to the unittest2 library on all Python versions. Since unittest2 became the standard library’s unittest module in Python 2.7, and Django 1.7 drops support for older Python versions, this module isn’t useful anymore. It has been deprecated. Use unittest instead.
As OrderedDict was added to the standard library in Python 2.7, SortedDict is no longer needed and has been deprecated.
Previously, if models were organized in a package (myapp/models/) rather than simply myapp/models.py, Django would look for initial SQL data in myapp/models/sql/. This bug has been fixed so that Django will search myapp/sql/ as documented. The old location will continue to work until Django 1.9.
django.contrib.sites provides reduced functionality when it isn’t in INSTALLED_APPS. The app-loading refactor adds some constraints in that situation. As a consequence, two objects were moved, and the old locations are deprecated:
ModelAdmin.declared_fieldsets has been deprecated. Despite being a private API, it will go through a regular deprecation path. This attribute was mostly used by methods that bypassed ModelAdmin.get_fieldsets() but this was considered a bug and has been addressed.
Since django.contrib.contenttypes.generic defined both admin and model related objects an import of this module could trigger unexpected side effects. As a consequence, its contents were split into contenttypes submodules and the django.contrib.contenttypes.generic module is deprecated:
The syncdb command has been deprecated in favor of the new migrate command. migrate takes the same arguments as syncdb used to plus a few more, so it’s safe to just change the name you’re calling and nothing else.
The following instances of util.py in the Django codebase have been renamed to utils.py in an effort to unify all util and utils references:
ModelAdmin.get_formsets has been deprecated in favor of the new get_formsets_with_inlines(), in order to better handle the case of selecting showing inlines on a ModelAdmin.
The django.db.models.IPAddressField and django.forms.IPAddressField fields have been deprecated in favor of django.db.models.GenericIPAddressField and django.forms.GenericIPAddressField.
The BaseMemcachedCache._get_memcache_timeout() method has been renamed to get_backend_timeout(). Despite being a private API, it will go through the normal deprecation.
The --natural and -n options for dumpdata have been deprecated. Use --natural-foreign instead.
Similarly, the use_natural_keys argument for serializers.serialize() has been deprecated. Use use_natural_foreign_keys instead.
It was already strongly suggested that you use GET and POST instead of REQUEST, because the former are more explicit. The property REQUEST is deprecated and will be removed in Django 1.9.
MergeDict exists primarily to support merging POST and GET arguments into a REQUEST property on WSGIRequest. To merge dictionaries, use dict.update() instead. The class MergeDict is deprecated and will be removed in Django 1.9.
The currently used language codes for Simplified Chinese zh-cn, Traditional Chinese zh-tw and (Western) Frysian fy-nl are deprecated and should be replaced by the language codes zh-hans, zh-hant and fy respectively. If you use these language codes, you should rename the locale directories and update your settings to reflect these changes. The deprecated language codes will be removed in Django 1.9.
The function memoize is deprecated and should be replaced by the functools.lru_cache decorator (available from Python 3.2 onwards).
Django ships a backport of this decorator for older Python versions and it’s available at django.utils.lru_cache.lru_cache. The deprecated function will be removed in Django 1.9.
Google has retired support for the Geo Sitemaps format. Hence Django support for Geo Sitemaps is deprecated and will be removed in Django 1.8.
Callable arguments for querysets were an undocumented feature that was unreliable. It’s been deprecated and will be removed in Django 1.9.
Callable arguments were evaluated when a queryset was constructed rather than when it was evaluated, thus this feature didn’t offer any benefit compared to evaluating arguments before passing them to queryset and created confusion that the arguments may have been evaluated at query time.
The ADMIN_FOR feature, part of the admindocs, has been removed. You can remove the setting from your configuration at your convenience.
SplitDateTimeWidget support in DateTimeField is deprecated, use SplitDateTimeWidget with SplitDateTimeField instead.
requires_model_validation is deprecated in favor of a new requires_system_checks flag. If the latter flag is missing, then the value of the former flag is used. Defining both requires_system_checks and requires_model_validation results in an error.
The check() method has replaced the old validate() method.
ModelAdmin.validator is deprecated in favor of new checks attribute.
This method is deprecated in favor of a new check_field method. The functionality required by check_field() is the same as that provided by validate_field(), but the output format is different. Third-party database backends needing this functionality should modify their backends to provide an implementation of check_field().
Django 1.3 introduced {% load ssi from future %} and {% load url from future %} syntax for forward compatibility of the ssi and url template tags. This syntax is now deprecated and will be removed in Django 1.9. You can simply remove the {% load ... from future %} tags.
javascript_quote() was an undocumented function present in django.utils.text. It was used internally in the javascript_catalog view whose implementation was changed to make use of json.dumps() instead. If you were relying on this function to provide safe output from untrusted strings, you should use django.utils.html.escapejs or the escapejs template filter. If all you need is to generate valid javascript strings, you can simply use json.dumps().
The django.utils.html.fix_ampersands method and the fix_ampersands template filter are deprecated, as the escaping of ampersands is already taken care of by Django’s standard HTML escaping features. Combining this with fix_ampersands would either result in double escaping, or, if the output is assumed to be safe, a risk of introducing XSS vulnerabilities. Along with fix_ampersands, django.utils.html.clean_html is deprecated, an undocumented function that calls fix_ampersands. As this is an accelerated deprecation, fix_ampersands and clean_html will be removed in Django 1.8.
Sep 05, 2014