Django models字段查询谓词表

谓词含义示例等价SQL语句
exact精确等于Comment.objects.filter(id__exact=14)select * from Comment where id=14
iexact大小写不敏感的等于Comment.objects.filter(headline__iexact=’I like this’)select * from Comment where upper(headline)=’I LIKE THIS’
contains模糊匹配Comment.objects.filter(headline__contains=’good’)select * from Comment where headline like “%good%”
in包含Comment.objects.filter(id__in=[1,5,9])select * from Comment where id in (1,5,9)
gt大于Comment.objects.filter(n_visits__gt=30)select * from Comment where n_visits>30
gte大于等于Comment.objects.filter(n_visits__gte=30)select * from COmment where n_visits>=30
lt小于  
lte小于等于  
startswith以…开头Comment.objects.filter(body_text__startswith=”Hello”)select * from Comment where body_text like ‘Hello%’
endswith以…结尾  
range在…范围内start_date=datetime.date(2015,1,1)
end_date=datetime.date(2015.2.1)
Comment.objects.filter(
    pub_date__range=(start_date,end_date)
)
select * from Comment
where pub_date
between ‘2015-1-1’ and ‘2015-2-1’
yearComment.objects.filter(
    pub_date__year=2015
)
select * from Comment
where pub_date
between ‘2015-1-1 0:0:0’
and ‘2015-12-31 23:59:59’
month  
day  
week_day星期几  
isnull是否为空Comment.objects.filter(
    pub_date__isnull=True
)
select * from Comment where
pub_date is NULL

 

filter(**kwargs): 返回符合筛选条件的数据集

exclude(**kwargs):   返回不符合筛选条件的数据集

Comment.objects.filter(pub_date__year=2015)

多个filter和exclude可以链接在一起查询

Comment.objects.filter(pub_date__year=2015).exclude(pub_date__month=1).exclude(n_visits__exact=0)

get() :查询单条记录,注意没有查询到数据的时候会报错

Comment.objects.get(id_exact=1)

all():  查询所有数据

Comment.objects.all()
Comment.objects.all()[:10]
Comment.objects.all[10:20]

order_by():   排序

Comment.objects.order_by('headline')

转载于:https://www.cnblogs.com/huangxm/p/6279979.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值