查询结果新增加一个自定义字段
使用
annotate
结合
Value
表达式
例如:新增type列,值均为ip
ThreatIP.objects.all().annotate(type=Value('ip', output_field=CharField()))
查询结果字段重命名
使用
annotate
结合
F
表达式
例如:将原始字段中的
inform_date
更改为
date
ThreatIP.objects.annotate(date=F('inform_date')).values("date")...
多个字段模糊查询
使用
filter
结合
Q
表达式结合
__contains
模糊查询
例如:查询
region
、
threat_type
、
threat_description
字段中包含value的数据
ThreatIP.objects.values("id""date").filter( Q(region__contains=value) | Q(threat_type__contains=value) | Q(threat_description__contains=value))