Django 1.11 release notes¶
April 4, 2017
Welcome to Django 1.11!
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.10 or older versions. We’ve begun the deprecation process for some features.
See the Upgrading Django to a newer version guide if you’re updating an existing project.
Django 1.11 is designated as a long-term support release. It will receive security updates for at least three years after its release. Support for the previous LTS, Django 1.8, will end in April 2018.
Python compatibility¶
Django 1.11 requires Python 2.7, 3.4, 3.5, 3.6, or 3.7 (as of 1.11.17). We highly recommend and only officially support the latest release of each series.
The Django 1.11.x series is the last to support Python 2. The next major release, Django 2.0, will only support Python 3.4+.
Deprecating warnings are no longer loud by default¶
Unlike older versions of Django, Django’s own deprecation warnings are no longer displayed by default. This is consistent with Python’s default behavior.
This change allows third-party apps to support both Django 1.11 LTS and Django 1.8 LTS without having to add code to avoid deprecation warnings.
Following the release of Django 2.0, we suggest that third-party app authors
drop support for all versions of Django prior to 1.11. At that time, you should
be able run your package’s tests using python -Wd so that deprecation
warnings do appear. After making the deprecation warning fixes, your app should
be compatible with Django 2.0.
What’s new in Django 1.11¶
Class-based model indexes¶
The new django.db.models.indexes module contains classes which ease
creating database indexes. Indexes are added to models using the
Meta.indexes option.
The Index class creates a b-tree index, as if you
used db_index on the model field or
index_together on the model Meta class.
It can be subclassed to support different index types, such as
GinIndex. It also allows defining the
order (ASC/DESC) for the columns of the index.
Template-based widget rendering¶
To ease customizing widgets, form widget rendering is now done using the template system rather than in Python. See The form rendering API.
You may need to adjust any custom widgets that you’ve written for a few backwards incompatible changes.
Subquery expressions¶
The new Subquery and
Exists database expressions allow creating
explicit subqueries. Subqueries may refer to fields from the outer queryset
using the OuterRef class.
Minor features¶
django.contrib.admin¶
ModelAdmin.date_hierarchycan now reference fields across relations.- The new
ModelAdmin.get_exclude()hook allows specifying the exclude fields based on the request or model instance. - The
popup_response.htmltemplate can now be overridden per app, per model, or by setting theModelAdmin.popup_response_templateattribute.
django.contrib.auth¶
- The default iteration count for the PBKDF2 password hasher is increased by 20%.
- The
LoginViewandLogoutViewclass-based views supersede the deprecatedlogin()andlogout()function-based views. - The
PasswordChangeView,PasswordChangeDoneView,PasswordResetView,PasswordResetDoneView,PasswordResetConfirmView, andPasswordResetCompleteViewclass-based views supersede the deprecatedpassword_change(),password_change_done(),password_reset(),password_reset_done(),password_reset_confirm(), andpassword_reset_complete()function-based views. - The new
post_reset_loginattribute forPasswordResetConfirmViewallows automatically logging in a user after a successful password reset. If you have multipleAUTHENTICATION_BACKENDSconfigured, use thepost_reset_login_backendattribute to choose which one to use. - To avoid the possibility of leaking a password reset token via the HTTP
Referer header (for example, if the reset page includes a reference to CSS or
JavaScript hosted on another domain), the
PasswordResetConfirmView(but not the deprecatedpassword_reset_confirm()function-based view) stores the token in a session and redirects to itself to present the password change form to the user without the token in the URL. update_session_auth_hash()now rotates the session key to allow a password change to invalidate stolen session cookies.- The new
success_url_allowed_hostsattribute forLoginViewandLogoutViewallows specifying a set of hosts that are safe for redirecting after login and logout. - Added password validators
help_texttoUserCreationForm. - The
HttpRequestis now passed toauthenticate()which in turn passes it to the authentication backend if it accepts arequestargument. - The
user_login_failed()signal now receives arequestargument. PasswordResetFormsupports custom user models that use an email field named something other than'email'. SetCustomUser.EMAIL_FIELDto the name of the field.get_user_model()can now be called at import time, even in modules that define models.
django.contrib.contenttypes¶
- When stale content types are detected in the
remove_stale_contenttypescommand, there’s now a list of related objects such asauth.Permissions that will also be deleted. Previously, only the content types were listed (and this prompt was aftermigraterather than in a separate command).
django.contrib.gis¶
- The new
GEOSGeometry.from_gml()andOGRGeometry.from_gml()methods allow creating geometries from GML. - Added support for the
dwithinlookup on SpatiaLite. - The
Areafunction,Distancefunction, and distance lookups now work with geodetic coordinates on SpatiaLite. - The OpenLayers-based form widgets now use
OpenLayers.jsfromhttps://cdnjs.cloudflare.comwhich is more suitable for production use than the oldhttps://openlayers.org/source. They are also updated to use OpenLayers 3. - PostGIS migrations can now change field dimensions.
- Added the ability to pass the size, shape, and offset parameter when
creating
GDALRasterobjects. - Added SpatiaLite support for the
IsValidfunction,MakeValidfunction, andisvalidlookup. - Added Oracle support for the
AsGMLfunction,BoundingCirclefunction,IsValidfunction, andisvalidlookup.
django.contrib.postgres¶
- The new
distinctargument forStringAggdetermines if concatenated values will be distinct. - The new
GinIndexandBrinIndexclasses allow creatingGINandBRINindexes in the database. JSONFieldaccepts a newencoderparameter to specify a custom class to encode data types not supported by the standard encoder.- The new
CITextmixin andCITextExtensionmigration operation allow using PostgreSQL’scitextextension for case-insensitive lookups. Three fields are provided:CICharField,CIEmailField, andCITextField. - The new
JSONBAggallows aggregating values as a JSON array. - The
HStoreField(model field) andHStoreField(form field) allow storing null values.
Cache¶
- Memcached backends now pass the contents of
OPTIONSas keyword arguments to the client constructors, allowing for more advanced control of client behavior. See the cache arguments documentation for examples. - Memcached backends now allow defining multiple servers as a comma-delimited
string in
LOCATION, for convenience with third-party services that use such strings in environment variables.
CSRF¶
- Added the
CSRF_USE_SESSIONSsetting to allow storing the CSRF token in the user’s session rather than in a cookie.
Database backends¶
- Added the
skip_lockedargument toQuerySet.select_for_update()on PostgreSQL 9.5+ and Oracle to execute queries withFOR UPDATE SKIP LOCKED. - Added the
TEST['TEMPLATE']setting to let PostgreSQL users specify a template for creating the test database. QuerySet.iterator()now uses server-side cursors on PostgreSQL. This feature transfers some of the worker memory load (used to hold query results) to the database and might increase database memory usage.- Added MySQL support for the
'isolation_level'option inOPTIONSto allow specifying the transaction isolation level. To avoid possible data loss, it’s recommended to switch from MySQL’s default level, repeatable read, to read committed. - Added support for
cx_Oracle5.3.
Email¶
- Added the
EMAIL_USE_LOCALTIMEsetting to allow sending SMTP date headers in the local time zone rather than in UTC. EmailMessage.attach()andattach_file()now fall back to MIME typeapplication/octet-streamwhen binary content that can’t be decoded as UTF-8 is specified for atext/*attachment.
File Storage¶
- To make it wrappable by
io.TextIOWrapper,Filenow has thereadable(),writable(), andseekable()methods.
Forms¶
- The new
CharField.empty_valueattribute allows specifying the Python value to use to represent “empty”. - The new
Form.get_initial_for_field()method returns initial data for a form field.
Internationalization¶
- Number formatting and the
NUMBER_GROUPINGsetting support non-uniform digit grouping.
Management Commands¶
- The new
loaddata --excludeoption allows excluding models and apps while loading data from fixtures. - The new
diffsettings --defaultoption allows specifying a settings module other than Django’s default settings to compare against. app_labels arguments now limit theshowmigrations --planoutput.
Migrations¶
- Added support for serialization of
uuid.UUIDobjects.
Models¶
- Added support for callable values in the
defaultsargument ofQuerySet.update_or_create()andget_or_create(). ImageFieldnow has a defaultvalidate_image_file_extensionvalidator. (This validator moved to the form field in Django 1.11.2.)- Added support for time truncation to
Truncfunctions. - Added the
ExtractWeekfunction to extract the week fromDateFieldandDateTimeFieldand exposed it through theweeklookup. - Added the
TruncTimefunction to truncateDateTimeFieldto its time component and exposed it through thetimelookup. - Added support for expressions in
QuerySet.values()andvalues_list(). - Added support for query expressions on lookups that take multiple arguments,
such as
range. - You can now use the
unique=Trueoption withFileField. - Added the
nulls_firstandnulls_lastparameters toExpression.asc()anddesc()to control the ordering of null values. - The new
Fexpressionbitleftshift()andbitrightshift()methods allow bitwise shift operations. - Added
QuerySet.union(),intersection(), anddifference().
Requests and Responses¶
- Added
QueryDict.fromkeys(). CommonMiddlewarenow sets theContent-Lengthresponse header for non-streaming responses.- Added the
SECURE_HSTS_PRELOADsetting to allow appending thepreloaddirective to theStrict-Transport-Securityheader. ConditionalGetMiddlewarenow adds theETagheader to responses.
Serialization¶
- The new
django.core.serializers.base.Serializer.stream_classattribute allows subclasses to customize the default stream. - The encoder used by the JSON serializer
can now be customized by passing a
clskeyword argument to theserializers.serialize()function. DjangoJSONEncodernow serializestimedeltaobjects (used byDurationField).
Templates¶
mark_safe()can now be used as a decorator.- The
Jinja2template backend now supports context processors by setting the'context_processors'option inOPTIONS. - The
regrouptag now returnsnamedtuples instead of dictionaries so you can unpack the group object directly in a loop, e.g.{% for grouper, list in regrouped %}. - Added a
resetcycletemplate tag to allow resetting the sequence of thecycletemplate tag. - You can now specify specific directories for a particular
filesystem.Loader.
Tests¶
- Added
DiscoverRunner.get_test_runner_kwargs()to allow customizing the keyword arguments passed to the test runner. - Added the
test --debug-modeoption to help troubleshoot test failures by setting theDEBUGsetting toTrue. - The new
django.test.utils.setup_databases()(moved fromdjango.test.runner) andteardown_databases()functions make it easier to build custom test runners. - Added support for
unittest.TestCase.subTest()’s when using thetest --paralleloption. DiscoverRunnernow runs the system checks at the start of a test run. Override theDiscoverRunner.run_checks()method if you want to disable that.
Validators¶
- Added
FileExtensionValidatorto validate file extensions andvalidate_image_file_extensionto validate image files.
Backwards incompatible changes in 1.11¶
django.contrib.gis¶
- To simplify the codebase and because it’s easier to install than when
contrib.giswas first released, GDAL is now a required dependency for GeoDjango. In older versions, it’s only required for SQLite. contrib.gis.mapsis removed as it interfaces with a retired version of the Google Maps API and seems to be unmaintained. If you’re using it, let us know.- The
GEOSGeometryequality operator now also compares SRID. - The OpenLayers-based form widgets now use OpenLayers 3, and the
gis/openlayers.htmlandgis/openlayers-osm.htmltemplates have been updated. Check your project if you subclass these widgets or extend the templates. Also, the new widgets work a bit differently than the old ones. Instead of using a toolbar in the widget, you click to draw, click and drag to move the map, and click and drag a point/vertex/corner to move it. - Support for SpatiaLite < 4.0 is dropped.
- Support for GDAL 1.7 and 1.8 is dropped.
- The widgets in
contrib.gis.forms.widgetsand the admin’sOpenLayersWidgetuse the form rendering API rather thanloader.render_to_string(). If you’re using a custom widget template, you’ll need to be sure your form renderer can locate it. For example, you could use theTemplatesSettingrenderer.
django.contrib.staticfiles¶
collectstaticmay now fail during post-processing when using a hashed static files storage if a reference loop exists (e.g.'foo.css'references'bar.css'which itself references'foo.css') or if the chain of files referencing other files is too deep to resolve in several passes. In the latter case, increase the number of passes usingManifestStaticFilesStorage.max_post_process_passes.- When using
ManifestStaticFilesStorage, static files not found in the manifest at runtime now raise aValueErrorinstead of returning an unchanged path. You can revert to the old behavior by settingManifestStaticFilesStorage.manifest_stricttoFalse.
Database backend API¶
This section describes changes that may be needed in third-party database backends.
- The
DatabaseOperations.time_trunc_sql()method is added to supportTimeFieldtruncation. It accepts alookup_typeandfield_namearguments and returns the appropriate SQL to truncate the given time fieldfield_nameto a time object with only the given specificity. Thelookup_typeargument can be either'hour','minute', or'second'. - The
DatabaseOperations.datetime_cast_time_sql()method is added to support thetimelookup. It accepts afield_nameandtznamearguments and returns the SQL necessary to cast a datetime value to time value. - To enable
FOR UPDATE SKIP LOCKEDsupport, setDatabaseFeatures.has_select_for_update_skip_locked = True. - The new
DatabaseFeatures.supports_index_column_orderingattribute specifies if a database allows defining ordering for columns in indexes. The default value isTrueand theDatabaseIntrospection.get_constraints()method should include an'orders'key in each of the returned dictionaries with a list of'ASC'and/or'DESC'values corresponding to the ordering of each column in the index. inspectdbno longer callsDatabaseIntrospection.get_indexes()which is deprecated. Custom database backends should ensure all types of indexes are returned byDatabaseIntrospection.get_constraints().- Renamed the
ignores_quoted_identifier_casefeature toignores_table_name_caseto more accurately reflect how it is used. - The
namekeyword argument is added to theDatabaseWrapper.create_cursor(self, name=None)method to allow usage of server-side cursors on backends that support it.
Dropped support for PostgreSQL 9.2 and PostGIS 2.0¶
Upstream support for PostgreSQL 9.2 ends in September 2017. As a consequence, Django 1.11 sets PostgreSQL 9.3 as the minimum version it officially supports.
Support for PostGIS 2.0 is also removed as PostgreSQL 9.2 is the last version to support it.
Also, the minimum supported version of psycopg2 is increased from 2.4.5 to 2.5.4.
LiveServerTestCase binds to port zero¶
Rather than taking a port range and iterating to find a free port,
LiveServerTestCase binds to port zero and relies on the operating system
to assign a free port. The DJANGO_LIVE_TEST_SERVER_ADDRESS environment
variable is no longer used, and as it’s also no longer used, the
manage.py test --liveserver option is removed.
If you need to bind LiveServerTestCase to a specific port, use the port
attribute added in Django 1.11.2.
Protection against insecure redirects in django.contrib.auth and i18n views¶
LoginView, LogoutView (and the deprecated function-based equivalents),
and set_language() protect users from being redirected
to non-HTTPS next URLs when the app is running over HTTPS.
QuerySet.get_or_create() and update_or_create() validate arguments¶
To prevent typos from passing silently,
get_or_create() and
update_or_create() check that their
arguments are model fields. This should be backwards-incompatible only in the
fact that it might expose a bug in your project.
pytz is a required dependency and support for settings.TIME_ZONE = None is removed¶
To simplify Django’s timezone handling, pytz is now a required dependency.
It’s automatically installed along with Django.
Support for settings.TIME_ZONE = None is removed as the behavior isn’t
commonly used and is questionably useful. If you want to automatically detect
the timezone based on the system timezone, you can use tzlocal:
from tzlocal import get_localzone
TIME_ZONE = get_localzone().zone
This works similar to settings.TIME_ZONE = None except that it also sets
os.environ['TZ']. Let us know
if there’s a use case where you find you can’t adapt your code to set a
TIME_ZONE.
HTML changes in admin templates¶
<p class="help"> is replaced with a <div> tag to allow including lists
inside help text.
Read-only fields are wrapped in <div class="readonly">...</div> instead of
<p>...</p> to allow any kind of HTML as the field’s content.
Changes due to the introduction of template-based widget rendering¶
Some undocumented classes in django.forms.widgets are removed:
SubWidgetRendererMixin,ChoiceFieldRenderer,RadioFieldRenderer,CheckboxFieldRendererChoiceInput,RadioChoiceInput,CheckboxChoiceInput
The undocumented Select.render_option() method is removed.
The Widget.format_output() method is removed. Use a custom widget template
instead.
Some widget values, such as <select> options, are now localized if
settings.USE_L10N=True. You could revert to the old behavior with custom
widget templates that uses the localize template tag to turn off
localization.
django.template.backends.django.Template.render() prohibits non-dict context¶
For compatibility with multiple template engines,
django.template.backends.django.Template.render() (returned from high-level
template loader APIs such as loader.get_template()) must receive a
dictionary of context rather than Context or RequestContext. If you
were passing either of the two classes, pass a dictionary instead – doing so
is backwards-compatible with older versions of Django.
Model state changes in migration operations¶
To improve the speed of applying migrations, rendering of related models is
delayed until an operation that needs them (e.g. RunPython). If you have a
custom operation that works with model classes or model instances from the
from_state argument in database_forwards() or database_backwards(),
you must render model states using the clear_delayed_apps_cache() method as
described in writing your own migration operation.
Server-side cursors on PostgreSQL¶
The change to make QuerySet.iterator() use server-side cursors on
PostgreSQL prevents running Django with pgBouncer in transaction pooling
mode. To reallow that, use the DISABLE_SERVER_SIDE_CURSORS setting (added in Django 1.11.1) in
DATABASES.
See Transaction pooling and server-side cursors for more discussion.
Miscellaneous¶
If no items in the feed have a
pubdateorupdateddateattribute,SyndicationFeed.latest_post_date()now returns the current UTC date/time, instead of a datetime without any timezone information.CSRF failures are logged to the
django.security.csrflogger instead ofdjango.request.ALLOWED_HOSTSvalidation is no longer disabled when running tests. If your application includes tests with custom host names, you must include those host names inALLOWED_HOSTS. See Tests and multiple host names.Using a foreign key’s id (e.g.
'field_id') inModelAdmin.list_displaydisplays the related object’s ID. Remove the_idsuffix if you want the old behavior of the string representation of the object.In model forms,
CharFieldwithnull=Truenow savesNULLfor blank values instead of empty strings.On Oracle,
Model.validate_unique()no longer checks empty strings for uniqueness as the database interprets the value asNULL.If you subclass
AbstractUserand overrideclean(), be sure it callssuper().BaseUserManager.normalize_email()is called in a newAbstractUser.clean()method so that normalization is applied in cases like model form validation.EmailFieldandURLFieldno longer accept thestripkeyword argument. Remove it because it doesn’t have an effect in older versions of Django as these fields always strip whitespace.The
checkedandselectedattribute rendered by form widgets now uses HTML5 boolean syntax rather than XHTML’schecked='checked'andselected='selected'.RelatedManager.add(),remove(),clear(), andset()now clear theprefetch_related()cache.To prevent possible loss of saved settings,
setup_test_environment()now raises an exception if called a second time before callingteardown_test_environment().The undocumented
DateTimeAwareJSONEncoderalias forDjangoJSONEncoder(renamed in Django 1.0) is removed.The
cached template loaderis now enabled ifOPTIONS['loaders']isn’t specified andOPTIONS['debug']isFalse(the latter option defaults to the value ofDEBUG). This could be backwards-incompatible if you have some template tags that aren’t thread safe.The prompt for stale content type deletion no longer occurs after running the
migratecommand. Use the newremove_stale_contenttypescommand instead.The admin’s widget for
IntegerFieldusestype="number"rather thantype="text".Conditional HTTP headers are now parsed and compared according to the RFC 7232 Conditional Requests specification rather than the older RFC 2616.
patch_response_headers()no longer adds aLast-Modifiedheader. According to the RFC 7234#section-4.2.2, this header is useless alongside other caching headers that provide an explicit expiration time, e.g.ExpiresorCache-Control.UpdateCacheMiddlewareandadd_never_cache_headers()callpatch_response_headers()and therefore are also affected by this change.In the admin templates,
<p class="help">is replaced with a<div>tag to allow including lists inside help text.ConditionalGetMiddlewareno longer sets theDateheader as Web servers set that header. It also no longer sets theContent-Lengthheader as this is now done byCommonMiddleware.If you have a middleware that modifies a response’s content and appears before
CommonMiddlewarein theMIDDLEWAREorMIDDLEWARE_CLASSESsettings, you must reorder your middleware so that responses aren’t modified afterContent-Lengthis set, or have the response modifying middleware reset theContent-Lengthheader.get_model()andget_models()now raiseAppRegistryNotReadyif they’re called before models of all applications have been loaded. Previously they only required the target application’s models to be loaded and thus could return models without all their relations set up. If you need the old behavior ofget_model(), set therequire_readyargument toFalse.The unused
BaseCommand.can_import_settingsattribute is removed.The undocumented
django.utils.functional.lazy_propertyis removed.For consistency with non-multipart requests,
MultiPartParser.parse()now leavesrequest.POSTimmutable. If you’re modifying thatQueryDict, you must now first copy it, e.g.request.POST.copy().Support for
cx_Oracle< 5.2 is removed.Support for IPython < 1.0 is removed from the
shellcommand.The signature of private API
Widget.build_attrs()changed fromextra_attrs=None, **kwargstobase_attrs, extra_attrs=None.File-like objects (e.g.,
StringIOandBytesIO) uploaded to anImageFieldusing the test client now require anameattribute with a value that passes thevalidate_image_file_extensionvalidator. See the note inClient.post().FileFieldnow moves rather than copies the file it receives. With the default file upload settings, files larger thanFILE_UPLOAD_MAX_MEMORY_SIZEnow have the same permissions as temporary files (often0o600) rather than the system’s standard umask (often0o6644). Set theFILE_UPLOAD_PERMISSIONSif you need the same permission regardless of file size.
Features deprecated in 1.11¶
models.permalink() decorator¶
Use django.urls.reverse() instead. For example:
from django.db import models
class MyModel(models.Model):
...
@models.permalink
def url(self):
return ('guitarist_detail', [self.slug])
becomes:
from django.db import models
from django.urls import reverse
class MyModel(models.Model):
...
def url(self):
return reverse('guitarist_detail', args=[self.slug])
Miscellaneous¶
contrib.auth’slogin()andlogout()function-based views are deprecated in favor of new class-based viewsLoginViewandLogoutView.- The unused
extra_contextparameter ofcontrib.auth.views.logout_then_login()is deprecated. contrib.auth’spassword_change(),password_change_done(),password_reset(),password_reset_done(),password_reset_confirm(), andpassword_reset_complete()function-based views are deprecated in favor of new class-based viewsPasswordChangeView,PasswordChangeDoneView,PasswordResetView,PasswordResetDoneView,PasswordResetConfirmView, andPasswordResetCompleteView.django.test.runner.setup_databases()is moved todjango.test.utils.setup_databases(). The old location is deprecated.django.utils.translation.string_concat()is deprecated in favor ofdjango.utils.text.format_lazy().string_concat(*strings)can be replaced byformat_lazy('{}' * len(strings), *strings).- For the
PyLibMCCachecache backend, passingpylibmcbehavior settings as top-level attributes ofOPTIONSis deprecated. Set them under abehaviorskey withinOPTIONSinstead. - The
hostparameter ofdjango.utils.http.is_safe_url()is deprecated in favor of the newallowed_hostsparameter. - Silencing exceptions raised while rendering the
{% include %}template tag is deprecated as the behavior is often more confusing than helpful. In Django 2.1, the exception will be raised. DatabaseIntrospection.get_indexes()is deprecated in favor ofDatabaseIntrospection.get_constraints().authenticate()now passes arequestargument to theauthenticate()method of authentication backends. Support for methods that don’t acceptrequestas the first positional argument will be removed in Django 2.1.- The
USE_ETAGSsetting is deprecated in favor ofConditionalGetMiddlewarewhich now adds theETagheader to responses regardless of the setting.CommonMiddlewareanddjango.utils.cache.patch_response_headers()will no longer set ETags when the deprecation ends. Model._meta.has_auto_fieldis deprecated in favor of checking ifModel._meta.auto_field is not None.- Using regular expression groups with
iLmsu#inurl()is deprecated. The only group that’s useful is(?i)for case-insensitive URLs, however, case-insensitive URLs aren’t a good practice because they create multiple entries for search engines, for example. An alternative solution could be to create ahandler404that looks for uppercase characters in the URL and redirects to a lowercase equivalent. - The
rendererargument is added to theWidget.render()method. Methods that don’t accept that argument will work through a deprecation period.