基于双下划线的模糊查询
#1 价格在[100,200,300]这个范围内
Book.objects.filter(price__in=[100,200,300])
# 2 大于,小于,大于等于,小于等于
Book.objects.filter(price__gt=100)
Book.objects.filter(price__lt=100)
Book.objects.filter(price__gte=100)
Book.objects.filter(price__lte=100)
# 3 范围
Book.objects.filter(price__range=[100,200])
# 包含
Book.objects.filter(title__contains="python")
# 4 忽略大小写包含
Book.objects.filter(title__icontains="python")
# 5 以xx开头
Book.objects.filter(title__startswith="py")
# 6 时间类型,年份是2012年的
Book.objects.filter(pub_date__year=2012)
or
res=models.User.objests.filter(register_time__year='2012')
按照月份拿数据:按照月份为1月的过滤数据
res=models.User.objests.filter(register_time__month='1') print(res)
# 基于双下滑线的模糊查询
# 1 in条件
# res=models.Book.objects.filter(name__in=['西游记', '红楼梦'])
# print(res)
# 2 大于,小于,大于等于,小于等于
# res=models.Book.objects.filter(id__gt=2)
# res=models.Book.objects.filter(id__lt=2)
# res=models.Book.objects.filter(id__gte=2)
# res=models.Book.objects.filter(id__lte=2)
# print(res)
# 3 rang 范围 BETWEEN 1 AND 3
# res=models.Book.objects.filter(id__range=[1,3])
# print(res)
# 包含
# res=models.Book.objects.filter(name__contains="红")
# print(res)
# 忽略大小写的包含
# res=models.Book.objects.filter(name__icontains="红")
# print(res)
#
# res=models.Book.objects.filter(name__startswith="红")
# print(res)
# res = models.Book.objects.filter(name__endswith='梦')
# print(res)
#
# res=models.Book.objects.filter(publish_date__year='2020')
# res=models.Book.objects.filter(publish_date__month='9')
# print(res)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南