The last two years, Django version upgrade much faster, 2.0 still did not take long, will soon be up to 3.0.
Let's take a look at the official roadmap and timetable:
version number | Release date | Updated stop | Stop modification date |
---|---|---|---|
3.0 | 2019-12 | 2020-08 | 2021-04 |
3.1 | 2020-08 | 2021-04 | 2021-12 |
3.2 LTS | 2021-04 | 2021-12 | 2024-04 |
We can see that in December 2019 the upcoming release 3.0, upgrade to 4.0 in 2022, but we still feel 2.2 yesterday.
This gives the majority of users and learners bring a certain amount of pressure to learn and upgrade costs, but also on behalf of Django's very healthy ecosystem, users increased steadily, rapid iteration function, is a good sign, we should actively embrace change, refuse behind.
Next, let's look at what new things and new changes in 3.0 exactly.
Python compatibility
The following version 3.6 is no longer supported, supports only 3.6,3.7,3.8 or later.
(Django's strategy has always been in accordance with the latest Django Please use the latest python.)
ASGI
This is the biggest change and enhance Django3.0!
By supporting ASGI, Django3.0 start with a full-duplex asynchronous communication capabilities to address their biggest shortcomings, no longer being criticized! (Tornado supporters say take this thing for two days is not the day)
Support ASGI not replace existing WSGI, but a supplement, which means that we now have two servers running the model.
It should be noted that only started after ASGI-based server, it supports asynchronous communication functions. In ASGI mode, and supports both asynchronous and synchronous communication. (WSGI the synchronous communication mode only)
Django official document of a deployment ASGI https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ , is worth learning about.
The introduction of ASGI also brought some things to consider: Django now monitors asynchronous event loop, and prevents you from asynchronous call is marked in the context 异步不安全
of the code, such as ORM operations (note that this is not to say that ORM can not be used). If you encounter SynchronousOnlyOperation
an error, please double-check code and all database operations moved to the sub-thread synchronization.
MariaDB
Support MariaDB 10.1 or later.
PostgreSQL
By adding a model Meta.constraints
option, you can add exclusion constraints in PostgreSQL database.
Filter expression
You can now use Boolean expressions directly output QuerySet filtering operation, without first polymerized and then filtered.
Model field enumerable options
New enumeration class TextChoices
, IntegerChoices
and Choices
they are all Field.choices
subclasses. That you did not have in CharField
adding the choices
parameters, you can create a direct TextChoices
object.
Small function
The following are some minor changes (not some unusual)
django.contrib.admin
- To
ModelAdmin.list_display
addadmin_order_field
support - The new
ModelAdmin.get_inlines()
method allows to specify a request or according to the model instanceinline
mode - Select2 database upgrade from version 4.0.3 to 4.0.7
- jQuery upgrade from version 3.3.1 to 3.4.1.
django.contrib.auth
PasswordResetConfirmView
You can add newreset_url_token
properties- New
BaseBackend
classes to simplify custom backend authentication. - New
get_user_permissions()
method, a mirror image of conventionalget_group_permissions()
methods. - Username HTML pages, widgets email and password fields added to
autocomplete
attribute to better interact with the browser device. - When executing
createsuperuser
a command, if you do not provide the appropriate command line parameters in non-interactive mode, you will be returned to the environment variables and password required field. ManyToManyField
You can now useREQUIRED_FIELDS
.- The new
UserManager.with_perm()
method will return the user has specified permission. The default password hashes PBKDF2 iteration count is increased to 180,000 from 150,000.
django.contrib.sessions
The new get_session_cookie_age()
method allows to dynamically specify the lifetime of the session cookie.
Models
New database hash function MD5, SHA1, SHA224, SHA256, SHA384, and SHA512.
Add a Sign database functions.
FilePathField
Now accepts callable objectsSelf-referencing
ManyToManyField
field allows symmetric intermediate tableAvg
AndSum
aggregation are now supporteddistinct
parameters, deduplication.Add
SmallAutoField
field, similar to theAutoField
field, except that it allows only a small range of values, for example from 1 to 32767, so it would be more secure.AutoField
,BigAutoField
AndSmallAutoField
it is inheritedIntegerField
,BigIntegerField
andSmallIntegerField
, so the verifier system checks and accordingly are inherited.FileField.upload_to
Now supportedpathlib.Path
.CheckConstraint
It is supported on MySQL 8.0.16+.
Django3.0 not backward compatible
(Brief overview)
- The following PostgreSQL 9.5 is no longer supported
- 12.2 The following Oracle no longer supports
- Delete the official Python 2 compatibility related API
- Some security settings now have a more secure default:
X_FRAME_OPTIONS
now defaults'DENY'
.SECURE_CONTENT_TYPE_NOSNIFF
Now by defaultTrue
. - 1.2.0 The following are no longer supported
pywatchman
library
Django3.0 discarded some functions
django.utils.encoding.force_text() and smart_text()
Some of django.utils and http, translation, text and other related content.
Django 3.0 deleted function
The following methods, properties and settings are deleted:
django.db.backends.postgresql_psycopg2
Moduledjango.shortcuts.render_to_response()
methodDEFAULT_CONTENT_TYPE
Set upHttpRequest.xreadlines()
methodField.from_db_value()
AndExpression.convert_value()
methodsQuerySet.earliest()
Andlatest()
thefield_name
keyword argumentsdjango.utils.http.cookie_date()
methodstaticfiles
Andadmin_static
template tag librarydjango.contrib.staticfiles.templatetags.staticfiles.static()
method
to sum up
We can see from the release notes, the core supports ASGI change Django3.0 is asynchronous communication, other minor changes are generally basic user experience does not come out.
Asynchronous communication function is said to be the django-channle this external libraries included in, the official Django as one of the core functions of the standard. Anyway, HTML5 websocket will be the direct communication and the Django. Django-based web to achieve chat rooms, web QQ, web pages and other micro-channel real-time communication applications will become simple, and the length of the polling we can say goodbye.
(More relevant content and Django recent changes, please pay attention to my official website www.liujiangblog.com, related to the latest instructional video will also be released the first time!)