- Django 2.0 版本发行说明
- Django 2.0 版本发行说明
- Python 兼容性
- Third-party library support for older version of Django
- Django 2.0 新特性
- Backwards incompatible changes in 2.0
- Removed support for bytestrings in some places
- 数据库后端 API
- Dropped support for Oracle 11.2
- Default MySQL isolation level is read committed
- AbstractUser.last_name max_length increased to 150
- QuerySet.reverse() and last() are prohibited after slicing
- Form fields no longer accept optional arguments as positional arguments
- call_command() validates the options it receives
- Indexes no longer accept positional arguments
- Foreign key constraints are now enabled on SQLite
- 杂项
- 在 2.0 中被废弃的功能
- Features removed in 2.0
Django 2.0 版本发行说明
Django 2.0 版本发行说明
2017 年 12 月 2 日
欢迎来到 Django 2.0 版本!
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.11 or earlier. We’ve dropped some features that have reached the end of their deprecation cycle, and we’ve begun the deprecation process for some features.
This release starts Django’s use of a loose form of semantic versioning, but there aren’t any major backwards incompatible changes that might be expected of a 2.0 release. Upgrading should be a similar amount of effort as past feature releases.
如果你要更新现有的项目,请看 如何将 Django 更新至新的版本 指南。
Python 兼容性
Django 2.0 supports Python 3.4, 3.5, 3.6, and 3.7. We highly recommend and only officially support the latest release of each series.
Django 1.11.x 系列是支持 Python 2.7 版本的最后一个系列。
Django 2.0 will be the last release series to support Python 3.4. If you plan a deployment of Python 3.4 beyond the end-of-life for Django 2.0 (April 2019), stick with Django 1.11 LTS (supported until April 2020) instead. Note, however, that the end-of-life for Python 3.4 is March 2019.
Third-party library support for older version of Django
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 to 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.
Django 2.0 新特性
简化 URL 路由语法
新版本 django.urls.path() 函数使用更简单易读的 URL 路由语法。一个来自 Django 旧版本的例子:
url(r"^articles/(?P<year>[0-9]{4})/$", views.year_archive),
could be written as:
path("articles/<int:year>/", views.year_archive),
新版本语法支持对 URL 参数的强制类型转换。在这个例子中,视图将会以数字类型接收参数 year 而不是以字符串类型。而且 URLs 的匹配限制也减少了。例如,因为参数 year 并没有像正则表达式中那样明确限制为四位数,year 为 10000 也会匹配成功。
旧版本中的 django.conf.urls.url() 函数现在可以在 django.urls.re_path() 中获取。前者依然保留,向后兼容,并没有立即弃用。旧版本中的 django.conf.urls.include() 函数现在可以从 django.urls 中导入,这样你就可以在 URLconfs 中使用 from django.urls import include, path, re_path 。
重新编写的文档 URL调度器 涵盖了新的语法并且提供了更多的细节。
contrib.admin 针对移动设备的优化改进
新版本的 admin 可以响应和支持所有主流移动设备。较旧的浏览器可能会遇到不同程度的功能降低。
Window 表达式
The new Window expression allows adding an OVER clause to querysets. You can use window functions and aggregate functions in the expression.
次要特性
django.contrib.admin
- The new ModelAdmin.autocomplete_fields attribute and ModelAdmin.get_autocomplete_fields() method allow using a Select2 search widget for
ForeignKeyandManyToManyField.
django.contrib.auth
- PBKDF2 密码散列器的默认迭代计数从 36,000 增加到 100,000。
django.contrib.gis
- 增加了 AsGeoJSON 函数、 GeoHash 函数、IsValid 函数、isvalid 查找、以及 distance lookups 对 MySQL 的支持。
- 添加了 PostGIS 和 SpatiaLite 支持的两个函数 Azimuth 和 LineLocatePoint。
- 现在所有从 GeoJSON 导入的 GEOSGeometry 都设置了其 SRID。
- 添加了 OSMWidget.default_zoom 属性来自定义地图的默认缩放级别。
- Made metadata readable and editable on rasters through the metadata, info, and metadata attributes.
- 允许使用
papsz_options将 driver-specific creation options 传递给 GDALRaster 对象。 - Allowed creating GDALRaster objects in GDAL’s internal virtual filesystem. Rasters can now be created from and converted to binary data in-memory.
- The new GDALBand.color_interp() method returns the color interpretation for the band.
django.contrib.postgres
- The new
distinctargument for ArrayAgg determines if concatenated values will be distinct. - The new RandomUUID database function returns a version 4 UUID. It requires use of PostgreSQL’s
pgcryptoextension which can be activated using the new CryptoExtension migration operation. - django.contrib.postgres.indexes.GinIndex now supports the
fastupdateandgin_pending_list_limitparameters. - The new GistIndex class allows creating
GiSTindexes in the database. The new BtreeGistExtension migration operation installs thebtree_gistextension to add support for operator classes that aren’t built-in. - inspectdb can now introspect
JSONFieldand variousRangeFields (django.contrib.postgresmust be inINSTALLED_APPS).
django.contrib.sitemaps
- Added the
protocolkeyword argument to the GenericSitemap constructor.
缓存
cache.set_many()now returns a list of keys that failed to be inserted. For the built-in backends, failed inserts can only happen on memcached.
文件存储
- File.open() can be used as a context manager, e.g.
with file.open() as f:.
表单
- The new
date_attrsandtime_attrsarguments for SplitDateTimeWidget and SplitHiddenDateTimeWidget allow specifying different HTML attributes for theDateInputandTimeInput(or hidden) subwidgets. - The new Form.errors.get_json_data() method returns form errors as a dictionary suitable for including in a JSON response.
通用视图
- 新属性 ContextMixin.extra_context 允许在
View.as_view()添加上下文。
管理命令
- inspectdb now translates MySQL’s unsigned integer columns to
PositiveIntegerFieldorPositiveSmallIntegerField. - The new makemessages —add-location option controls the comment format in
.pofiles. - loaddata can now read from stdin.
- The new diffsettings —output option allows formatting the output in a unified diff format.
- On Oracle, inspectdb can now introspect
AutoFieldif the column is created as an identity column. - On MySQL, dbshell now supports client-side TLS certificates.
迁移
- The new squashmigrations —squashed-name option allows naming the squashed migration.
模型
- The new StrIndex database function finds the starting index of a string inside another string.
- On Oracle,
AutoFieldandBigAutoFieldare now created as identity columns. - The new
chunk_sizeparameter of QuerySet.iterator() controls the number of rows fetched by the Python database client when streaming results from the database. For databases that don’t support server-side cursors, it controls the number of results Django fetches from the database adapter. - QuerySet.earliest(), QuerySet.latest(), and Meta.get_latest_by now allow ordering by several fields.
- Added the ExtractQuarter function to extract the quarter from DateField and DateTimeField, and exposed it through the quarter lookup.
- Added the TruncQuarter function to truncate DateField and DateTimeField to the first day of a quarter.
- Added the db_tablespace parameter to class-based indexes.
- If the database supports a native duration field (Oracle and PostgreSQL), Extract now works with DurationField.
- Added the
ofargument to QuerySet.select_for_update(), supported on PostgreSQL and Oracle, to lock only rows from specific tables rather than all selected tables. It may be helpful particularly when select_for_update() is used in conjunction with select_related(). - The new
field_nameparameter of QuerySet.in_bulk() allows fetching results based on any unique model field. - CursorWrapper.callproc() now takes an optional dictionary of keyword parameters, if the backend supports this feature. Of Django’s built-in backends, only Oracle supports it.
- The new connection.execute_wrapper() method allows installing wrappers around execution of database queries.
- The new
filterargument for built-in aggregates allows adding different conditionals to multiple aggregations over the same fields or relations. - Added support for expressions in Meta.ordering.
- The new
namedparameter of QuerySet.values_list() allows fetching results as named tuples. - The new FilteredRelation class allows adding an
ONclause to querysets.
分页
- Added Paginator.get_page() to provide the documented pattern of handling invalid page numbers.
请求和响应
- The runserver web server supports HTTP 1.1.
模板
- To increase the usefulness of Engine.get_default() in third-party apps, it now returns the first engine if multiple
DjangoTemplatesengines are configured inTEMPLATESrather than raisingImproperlyConfigured. - Custom template tags may now accept keyword-only arguments.
测试
- Added threading support to LiveServerTestCase.
- Added settings that allow customizing the test tablespace parameters for Oracle: DATAFILE_SIZE, DATAFILE_TMP_SIZE, DATAFILE_EXTSIZE, and DATAFILE_TMP_EXTSIZE.
验证器
- The new ProhibitNullCharactersValidator disallows the null character in the input of the CharField form field and its subclasses. Null character input was observed from vulnerability scanning tools. Most databases silently discard null characters, but psycopg2 2.7+ raises an exception when trying to save a null character to a char/text field with PostgreSQL.
Backwards incompatible changes in 2.0
Removed support for bytestrings in some places
To support native Python 2 strings, older Django versions had to accept both bytestrings and Unicode strings. Now that Python 2 support is dropped, bytestrings should only be encountered around input/output boundaries (handling of binary fields or HTTP streams, for example). You might have to update your code to limit bytestring usage to a minimum, as Django no longer accepts bytestrings in certain code paths. Python’s -b option may help detect that mistake in your code.
For example, reverse() now uses str() instead of force_text() to coerce the args and kwargs it receives, prior to their placement in the URL. For bytestrings, this creates a string with an undesired b prefix as well as additional quotes (str(b'foo') is "b'foo'"). To adapt, call decode() on the bytestring before passing it to reverse().
数据库后端 API
本节介绍了第三方数据库后端可能需要的更改。
- The
DatabaseOperations.datetime_cast_date_sql(),datetime_cast_time_sql(),datetime_trunc_sql(),datetime_extract_sql(), anddate_interval_sql()methods now return only the SQL to perform the operation instead of SQL and a list of parameters. - Third-party database backends should add a
DatabaseWrapper.display_nameattribute with the name of the database that your backend works with. Django may use it in various messages, such as in system checks. - The first argument of
SchemaEditor._alter_column_type_sql()is nowmodelrather thantable. - The first argument of
SchemaEditor._create_index_name()is nowtable_namerather thanmodel. - To enable
FOR UPDATE OFsupport, setDatabaseFeatures.has_select_for_update_of = True. If the database requires that the arguments toOFbe columns rather than tables, setDatabaseFeatures.select_for_update_of_column = True. - To enable support for Window expressions, set
DatabaseFeatures.supports_over_clausetoTrue. You may need to customize theDatabaseOperations.window_start_rows_start_end()and/orwindow_start_range_start_end()methods. - Third-party database backends should add a
DatabaseOperations.cast_char_field_without_max_lengthattribute with the database data type that will be used in the Cast function for aCharFieldif themax_lengthargument isn’t provided. - The first argument of
DatabaseCreation._clone_test_db()andget_test_db_clone_settings()is nowsuffixrather thannumber(in case you want to rename the signatures in your backend for consistency).django.testalso now passes those values as strings rather than as integers. - Third-party database backends should add a
DatabaseIntrospection.get_sequences()method based on the stub inBaseDatabaseIntrospection.
Dropped support for Oracle 11.2
The end of upstream support for Oracle 11.2 is Dec. 2020. Django 1.11 will be supported until April 2020 which almost reaches this date. Django 2.0 officially supports Oracle 12.1+.
Default MySQL isolation level is read committed
MySQL’s default isolation level, repeatable read, may cause data loss in typical Django usage. To prevent that and for consistency with other databases, the default isolation level is now read committed. You can use the DATABASES setting to use a different isolation level, if needed.
AbstractUser.last_name max_length increased to 150
A migration for django.contrib.auth.models.User.last_name is included. If you have a custom user model inheriting from AbstractUser, you’ll need to generate and apply a database migration for your user model.
If you want to preserve the 30 character limit for last names, use a custom form:
from django.contrib.auth.forms import UserChangeForm
class MyUserChangeForm(UserChangeForm):
last_name = forms.CharField(max_length=30, required=False)
如果你想在管理中编辑用户时保留这个限制,请将 UserAdmin.form 设置为使用这个表单:
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
QuerySet.reverse() and last() are prohibited after slicing
Calling QuerySet.reverse() or last() on a sliced queryset leads to unexpected results due to the slice being applied after reordering. This is now prohibited, e.g.:
>>> Model.objects.all()[:2].reverse()
Traceback (most recent call last):
...
TypeError: Cannot reverse a query once a slice has been taken.
Form fields no longer accept optional arguments as positional arguments
To help prevent runtime errors due to incorrect ordering of form field arguments, optional arguments of built-in form fields are no longer accepted as positional arguments. For example:
forms.IntegerField(25, 10)
raises an exception and should be replaced with:
forms.IntegerField(max_value=25, min_value=10)
call_command() validates the options it receives
call_command() now validates that the argument parser of the command being called defines all of the options passed to call_command().
For custom management commands that use options not created using parser.add_argument(), add a stealth_options attribute on the command:
class MyCommand(BaseCommand):
stealth_options = ("option_name", ...)
Indexes no longer accept positional arguments
例子:
models.Index(["headline", "-pub_date"], "index_name")
raises an exception and should be replaced with:
models.Index(fields=["headline", "-pub_date"], name="index_name")
Foreign key constraints are now enabled on SQLite
This will appear as a backwards-incompatible change (IntegrityError: FOREIGN KEY constraint failed) if attempting to save an existing model instance that’s violating a foreign key constraint.
Foreign keys are now created with DEFERRABLE INITIALLY DEFERRED instead of DEFERRABLE IMMEDIATE. Thus, tables may need to be rebuilt to recreate foreign keys with the new definition, particularly if you’re using a pattern like this:
from django.db import transaction
with transaction.atomic():
Book.objects.create(author_id=1)
Author.objects.create(id=1)
If you don’t recreate the foreign key as DEFERRED, the first create() would fail now that foreign key constraints are enforced.
Backup your database first! After upgrading to Django 2.0, you can then rebuild tables using a script similar to this:
from django.apps import apps
from django.db import connection
for app in apps.get_app_configs():
for model in app.get_models(include_auto_created=True):
if model._meta.managed and not (model._meta.proxy or model._meta.swapped):
for base in model.__bases__:
if hasattr(base, "_meta"):
base._meta.local_many_to_many = []
model._meta.local_many_to_many = []
with connection.schema_editor() as editor:
editor._remake_table(model)
This script hasn’t received extensive testing and needs adaption for various cases such as multiple databases. Feel free to contribute improvements.
In addition, because of a table alteration limitation of SQLite, it’s prohibited to perform RenameModel and RenameField operations on models or fields referenced by other models in a transaction. In order to allow migrations containing these operations to be applied, you must set the Migration.atomic attribute to False.
杂项
-
The
SessionAuthenticationMiddlewareclass is removed. It provided no functionality since session authentication is unconditionally enabled in Django 1.10. -
The default HTTP error handlers (
handler404, etc.) are now callables instead of dotted Python path strings. Django favors callable references since they provide better performance and debugging experience. -
RedirectView no longer silences
NoReverseMatchif thepattern_namedoesn’t exist. -
When
USE_L10Nis off, FloatField and DecimalField now respect DECIMAL_SEPARATOR and THOUSAND_SEPARATOR during validation. For example, with the settings:
USE_L10N = False
USE_THOUSAND_SEPARATOR = True
DECIMAL_SEPARATOR = ","
THOUSAND_SEPARATOR = "."
an input of "1.345" is now converted to 1345 instead of 1.345.
-
Subclasses of AbstractBaseUser are no longer required to implement
get_short_name()andget_full_name(). (The base implementations that raiseNotImplementedErrorare removed.)django.contrib.adminuses these methods if implemented but doesn’t require them. Third-party apps that use these methods may want to adopt a similar approach. -
The
FIRST_DAY_OF_WEEKandNUMBER_GROUPINGformat settings are now kept as integers in JavaScript and JSON i18n view outputs. -
assertNumQueries() now ignores connection configuration queries. Previously, if a test opened a new database connection, those queries could be included as part of the
assertNumQueries()count. -
The default size of the Oracle test tablespace is increased from 20M to 50M and the default autoextend size is increased from 10M to 25M.
-
To improve performance when streaming large result sets from the database, QuerySet.iterator() now fetches 2000 rows at a time instead of 100. The old behavior can be restored using the
chunk_sizeparameter. For example:
Book.objects.iterator(chunk_size=100)
-
Providing unknown package names in the
packagesargument of the JavaScriptCatalog view now raisesValueErrorinstead of passing silently. -
A model instance’s primary key now appears in the default
Model.__str__()method, e.g.Question object (1). -
makemigrationsnow detects changes to the model fieldlimit_choices_tooption. Add this to your existing migrations or accept an auto-generated migration for fields that use it. -
Performing queries that require automatic spatial transformations now raises
NotImplementedErroron MySQL instead of silently using non-transformed geometries. -
django.core.exceptions.DjangoRuntimeWarningis removed. It was only used in the cache backend as an intermediate class inCacheKeyWarning‘s inheritance ofRuntimeWarning. -
Renamed
BaseExpression._output_fieldtooutput_field. You may need to update custom expressions. -
In older versions, forms and formsets combine their
Mediawith widgetMediaby concatenating the two. The combining now tries to preserve the relative order of elements in each list.MediaOrderConflictWarningis issued if the order can’t be preserved. -
django.contrib.gis.gdal.OGRExceptionis removed. It’s been an alias forGDALExceptionsince Django 1.8. -
Support for GEOS 3.3.x is dropped.
-
The way data is selected for
GeometryFieldis changed to improve performance, and in raw SQL queries, those fields must now be wrapped inconnection.ops.select. See the Raw queries note in the GIS tutorial for an example.
在 2.0 中被废弃的功能
context argument of Field.from_db_value() and Expression.convert_value()
The context argument of Field.from_db_value() and Expression.convert_value() is unused as it’s always an empty dictionary. The signature of both methods is now:
(self, value, expression, connection)
替换成:
(self, value, expression, connection, context)
Support for the old signature in custom fields and expressions remains until Django 3.0.
杂项
- The
django.db.backends.postgresql_psycopg2module is deprecated in favor ofdjango.db.backends.postgresql. It’s been an alias since Django 1.9. This only affects code that imports from the module directly. TheDATABASESsetting can still use'django.db.backends.postgresql_psycopg2', though you can simplify that by using the'django.db.backends.postgresql'name added in Django 1.9. django.shortcuts.render_to_response()is deprecated in favor of django.shortcuts.render().render()takes the same arguments except that it also requires arequest.- The
DEFAULT_CONTENT_TYPEsetting is deprecated. It doesn’t interact well with third-party apps and is obsolete since HTML5 has mostly superseded XHTML. HttpRequest.xreadlines()is deprecated in favor of iterating over the request.- The
field_namekeyword argument to QuerySet.earliest() and QuerySet.latest() is deprecated in favor of passing the field names as arguments. Write.earliest('pub_date')instead of.earliest(field_name='pub_date').
Features removed in 2.0
These features have reached the end of their deprecation cycle and are removed in Django 2.0.
See 在 1.9 中被废弃的功能 for details on these changes, including how to remove usage of these features.
- The
weakargument todjango.dispatch.signals.Signal.disconnect()is removed. django.db.backends.base.BaseDatabaseOperations.check_aggregate_support()is removed.- The
django.forms.extraspackage is removed. - The
assignment_taghelper is removed. - The
hostargument toSimpleTestCase.assertsRedirects()is removed. The compatibility layer which allows absolute URLs to be considered equal to relative ones when the path is identical is also removed. Field.relandField.remote_field.toare removed.- The
on_deleteargument forForeignKeyandOneToOneFieldis now required in models and migrations. Consider squashing migrations so that you have fewer of them to update. django.db.models.fields.add_lazy_relation()is removed.- When time zone support is enabled, database backends that don’t support time zones no longer convert aware datetimes to naive values in UTC anymore when such values are passed as parameters to SQL queries executed outside of the ORM, e.g. with
cursor.execute(). django.contrib.auth.tests.utils.skipIfCustomUser()is removed.- The
GeoManagerandGeoQuerySetclasses are removed. - The
django.contrib.gis.geoipmodule is removed. - The
supports_recursioncheck for template loaders is removed from:django.template.engine.Engine.find_template()django.template.loader_tags.ExtendsNode.find_template()django.template.loaders.base.Loader.supports_recursion()django.template.loaders.cached.Loader.supports_recursion()
- The
load_templateandload_template_sourcestemplate loader methods are removed. - The
template_dirsargument for template loaders is removed:django.template.loaders.base.Loader.get_template()django.template.loaders.cached.Loader.cache_key()django.template.loaders.cached.Loader.get_template()django.template.loaders.cached.Loader.get_template_sources()django.template.loaders.filesystem.Loader.get_template_sources()
django.template.loaders.base.Loader.__call__()is removed.- Support for custom error views that don’t accept an
exceptionparameter is removed. - The
mime_typeattribute ofdjango.utils.feedgenerator.Atom1Feedanddjango.utils.feedgenerator.RssFeedis removed. - The
app_nameargument toinclude()is removed. - Support for passing a 3-tuple (including
admin.site.urls) as the first argument toinclude()is removed. - Support for setting a URL instance namespace without an application namespace is removed.
Field._get_val_from_obj()is removed.django.template.loaders.eggs.Loaderis removed.- The
current_appparameter to thecontrib.authfunction-based views is removed. - The
callable_objkeyword argument toSimpleTestCase.assertRaisesMessage()is removed. - Support for the
allow_tagsattribute onModelAdminmethods is removed. - The
enclosurekeyword argument toSyndicationFeed.add_item()is removed. - The
django.template.loader.LoaderOriginanddjango.template.base.StringOriginaliases fordjango.template.base.Originare removed.
See 在 1.10 中被废弃的功能 for details on these changes.
- The
makemigrations --exitoption is removed. - Support for direct assignment to a reverse foreign key or many-to-many relation is removed.
- The
get_srid()andset_srid()methods ofdjango.contrib.gis.geos.GEOSGeometryare removed. - The
get_x(),set_x(),get_y(),set_y(),get_z(), andset_z()methods ofdjango.contrib.gis.geos.Pointare removed. - The
get_coords()andset_coords()methods ofdjango.contrib.gis.geos.Pointare removed. - The
cascaded_unionproperty ofdjango.contrib.gis.geos.MultiPolygonis removed. django.utils.functional.allow_lazy()is removed.- The
shell --plainoption is removed. - The
django.core.urlresolversmodule is removed in favor of its new location,django.urls. CommaSeparatedIntegerFieldis removed, except for support in historical migrations.- The template
Context.has_key()method is removed. - Support for the
django.core.files.storage.Storage.accessed_time(),created_time(), andmodified_time()methods is removed. - Support for query lookups using the model name when
Meta.default_related_nameis set is removed. - The MySQL
__searchlookup is removed. - The shim for supporting custom related manager classes without a
_apply_rel_filters()method is removed. - Using
User.is_authenticated()andUser.is_anonymous()as methods rather than properties is no longer supported. - The
Model._meta.virtual_fieldsattribute is removed. - The keyword arguments
virtual_onlyinField.contribute_to_class()andvirtualinModel._meta.add_field()are removed. - The
javascript_catalog()andjson_catalog()views are removed. django.contrib.gis.utils.precision_wkt()is removed.- In multi-table inheritance, implicit promotion of a
OneToOneFieldto aparent_linkis removed. - Support for
Widget._format_value()is removed. FileFieldmethodsget_directory_name()andget_filename()are removed.- The
mark_for_escaping()function and the classes it uses:EscapeData,EscapeBytes,EscapeText,EscapeString, andEscapeUnicodeare removed. - The
escapefilter now usesdjango.utils.html.conditional_escape(). Manager.use_for_related_fieldsis removed.- Model
Managerinheritance follows MRO inheritance rules. The requirement to useMeta.manager_inheritance_from_futureto opt-in to the behavior is removed. - Support for old-style middleware using
settings.MIDDLEWARE_CLASSESis removed.
