原文档地址:docs.djangoproject.com/en/2.2/rele…html
能够在 model 的 Meta 中定义一个 constrains 列表。好比下面这个例子,添加了一个age
字段数据必须大于等于18的限制。数据库
注意:不能在 AbstractModel 类里面定义constrains,由于每一个 constrain都必需要有一个惟一的名字。若是在AbstractModel中定义的话,势必会重复。django
from django.db import models
class Customer(models.Model):
age = models.IntegerField()
class Meta:
constraints = [
models.CheckConstraint(check=models.Q(age__gte=18), name='age_gte_18'),
]
复制代码
models.Q 能够用来组合产生复杂的查询语句,好比 OR 查询:bash
queryset = User.objects.filter(
Q(first_name__startswith='R') | Q(last_name__startswith='D')
)
复制代码
AND 查询:post
queryset = User.objects.filter(
Q(first_name__startswith='R') & Q(last_name__startswith='D')
)
复制代码
first_name
以‘R’
开头, 可是 last_name
不以Z
开头:测试
queryset = User.objects.filter(
Q(first_name__startswith='R') & ~Q(last_name__startswith='Z')
)
复制代码
详情情见:优化
必填参数。spa
须要传入一个Q
对象,代表你须要怎样的 constrain 要求,好比CheckConstraint(check=Q(age__gte=18), name='age_gte_18')
确保age
字段用于不会小于18.命令行
必填参数。必须是惟一的。日志
对哪些 fields 作惟一限制。好比UniqueConstraint(fields=['room', 'date'], name='unique_booking')
确保每一个 room 一天只能被预订一次。
同上,必须是惟一的。
知足什么条件时才要求强制 constrain 条件。
测试用例:
2.2发布日志里面一个不起眼的地方写了这样一句:
Django no longer always starts a transaction when a single query is being performed, such as Model.save(), QuerySet.update(), and Model.delete(). This improves the performance of autocommit by reducing the number of database round trips.
也就是说 django 再也不和以前同样,每一个 query(好比 save,update,delete) 都会开启一个 transaction。这样能够经过减小 django <-> 数据库往返次数来提升效率。
考虑到这样的应用场景:table 很大,可是 query 只会查询一小部分的数据。为全部数据项都建一个索引是不必的,这时候就能够针对某一部分特定数据创建索引。
好比下面这个例子:
indexes = [
models.Index(
fields=['username'],
condition=models.Q(age__gte=30),
name='idx_username_age_gte30'
)
]
复制代码
将只会为年龄大于30岁的用户在username
这个字段上创建索引。
Index.condition 底层支持依赖于数据库的partial indexes。 而MySQL、MariaDB、Oracle都不支持partial indexes,因此这些数据库会直接忽略掉。
增长了一个ignore_conflicts
参数,设置为 TRUE 的时候告诉数据库忽略因为 constrain 产生的错误。
bulk_update(objs, fields, batch_size=None)
须要 update 大量数据的时候颇有用。
>>> objs = [
... Entry.objects.create(headline='Entry 1'),
... Entry.objects.create(headline='Entry 2'),
... ]
>>> objs[0].headline = 'This is entry 1'
>>> objs[1].headline = 'This is entry 2'
>>> Entry.objects.bulk_update(objs, ['headline'])
复制代码
这样会比使用一个 for 循环一个一个调用update()
方法速度更快。
有几点须要注意:
save()
方法不会被调用,因此post_save
和pre_save
信号将不会触发。batch_szie 参数
新增了一个--plan
命令行参数,用来查看 django 将要执行什么数据库修改操做。
django.contrib.postgres
ordering
参数增长了ArrayAgg
和 StringAgg
。能够针对 aggv 数据来作排序。BTreeIndex
, HashIndex
和 SpGistIndex
类容许建立 B-Tree, hash, and SP-GiST 索引。BrinIndex
如今有了一个autosummarize
参数。SearchQuery
的search_type
作了些变化。新增了HttpRequest.headers
,以便更方便地获取请求头信息。
>>> request.headers
{'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6', ...}
>>> 'User-Agent' in request.headers
True
>>> 'user-agent' in request.headers
True
>>> request.headers['User-Agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers['user-agent']
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('User-Agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
>>> request.headers.get('user-agent')
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)
复制代码
以前须要经过 request.META获取,相对要麻烦了不少。在2.2以前的版本,若是你想要获取全部的 HTTP 请求头的话,能够这么获取:
import re
regex = re.compile('^HTTP_')
dict((regex.sub('', header), value) for (header, value)
in request.META.items() if header.startswith('HTTP_'))
复制代码
其余琐碎的东西还有不少,好比:
collectstatic --ignore PATTERN
选项,忽略指定模式的静态资源。inspectdb
有了更丰富的功能。Tests
新功能。详细的,仍是去看官方的文档吧。此外还提到了一些往前不兼容的改变,若是你的项目里面用到了,也须要注意注意。
个人公众号:全栈不存在的