美文网首页
【转载】Django: 使用 Q 对象构建复杂的查询语句

【转载】Django: 使用 Q 对象构建复杂的查询语句

作者: 美滋滋只认钱 | 来源:发表于2017-11-28 11:28 被阅读0次

原文连接:https://mozillazg.github.io/2015/11/django-the-power-of-q-objects-and-how-to-use-q-object.html

AND 查询¶

OR 查询¶

NOT 查询¶

与关键字参数共用¶

OR, AND, NOT 多条件查询¶

动态构建查询条件¶

参考资料¶

本文将讲述如何在 Django 项目中使用Q对象构建复杂的查询条件。 假设有如下的 model:

classQuestion(models.Model):question_text=models.CharField(max_length=200)pub_date=models.DateTimeField('date published')

然后我们创建了一些数据:

Question.objects.create(question_text='what are you doing',pub_date=datetime.datetime(2015,11,7))Question.objects.create(question_text='what is wrong with you',pub_date=datetime.datetime(2014,11,7))Question.objects.create(question_text='who are you',pub_date=datetime.datetime(2015,10,7))Question.objects.create(question_text='who am i',pub_date=datetime.datetime(2014,10,7))>>>Question.objects.all()[,,,]

AND 查询

将多个Q对象作为非关键参数或使用&联结即可实现AND查询:

>>>fromdjango.db.modelsimportQ# Q(...)>>>Question.objects.filter(Q(question_text__contains='you'))[,,]# Q(...), Q(...)>>>Question.objects.filter(Q(question_text__contains='you'),Q(question_text__contains='what'))[,]# Q(...) & Q(...)>>>Question.objects.filter(Q(question_text__contains='you')&Q(question_text__contains='what'))[,]

OR 查询

使用|联结两个Q对象即可实现OR查询:

# Q(...) | Q(...)>>>Question.objects.filter(Q(question_text__contains='you')|Q(question_text__contains='who'))[,,,]

NOT 查询

使用~Q(...)客户实现NOT查询:

# ~Q(...)>>>Question.objects.filter(~Q(question_text__contains='you'))[]

与关键字参数共用

记得要把Q对象放前面:

# Q(...), key=value>>>Question.objects.filter(Q(question_text__contains='you'),question_text__contains='who')[]

OR, AND, NOT 多条件查询

这几个条件可以自由组合使用:

# (A OR B) AND C AND (NOT D)>>>Question.objects.filter((Q(question_text__contains='you')|Q(question_text__contains='who'))&Q(question_text__contains='what')&~Q(question_text__contains='are'))[]

动态构建查询条件

比如你定义了一个包含一些Q对象的列表,如何使用这个列表构建AND或OR查询呢? 可以使用operator和reduce:

>>>lst=[Q(question_text__contains='you'),Q(question_text__contains='who')]# OR>>>Question.objects.filter(reduce(operator.or_,lst))[,,,]# AND>>>Question.objects.filter(reduce(operator.and_,lst))[]

这个列表也可能是根据用户的输入来构建的,比如简单的搜索功能(搜索一个文章的标题或内容或作者名称包含某个关键字):

q=request.GET.get('q','').strip()lst=[]ifq:forkeyin['title__contains','content__contains','author__name__contains']:q_obj=Q(**{key:q})lst.append(q_obj)queryset=Entry.objects.filter(reduce(operator.or_,lst))

参考资料

Parerga und Paralipomena » Blog Archive » The power of django’s Q objects

Making queries | Django documentation | Django

QuerySet API reference | Django documentation | Django

django/tests.py at master · django/django · GitHub

9.9. operator — Standard operators as functions — Python 2.7.10 documentation

2. Built-in Functions — Python 2.7.10 documentation

相关文章

  • 【转载】Django: 使用 Q 对象构建复杂的查询语句

    原文连接:https://mozillazg.github.io/2015/11/django-the-power...

  • 数据库基本操作Q和F

    Q和F F使用查询条件的值,专门取对象中某列值的操作 Q 构建搜索条件 通常,Q()对象使得定义查询条件然后重用成...

  • 查看Django ORM执行的SQL语句

    查询QuerySet对象执行的SQL语句 查询当前执行的SQL包括Django内置执行的多条执行的SQL语句

  • Django: 查询

    使用Q进行复杂的查询 模糊查询 精确匹配 聚合查询

  • django查询之Q对象

    Q()对象和F()对象类似,把一个sql表达式封装在对象中,这个对象可以用于数据库相关的操作注意这里提醒一下,如果...

  • 建造者模式

    概念:使用多个复杂的对象构造成一个复杂的对象。 目的:将复杂的构建与表现相分离,使同样的构建可以构建不同的表示。-...

  • 3.7-URISearch详解

    URI Search - 通过URI query实现搜索 q 指定查询语句,使用Query String Synt...

  • MySQL高级查询——连接查询实例详解

    前言 我们使用SQL查询不能只使用很简单、最基础的SELECT语句查询。如果想从多个表查询比较复杂的信息,就会使用...

  • Prisma API:查询(Queries)

    本文属使用Prisma构建GraphQL服务系列。 Prisma API提供两种查询: 对象查询:获取某个对象类型...

  • MySQL 高级查询

    我们使用SQL查询不能只使用很简单、最基础的SELECT语句查询。如果想从多个表查询比较复杂的信息,就会使用高级查...

网友评论

      本文标题:【转载】Django: 使用 Q 对象构建复杂的查询语句

      本文链接:https://www.haomeiwen.com/subject/yyywvxtx.html