微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django中Q查询及Q()对象

<div id="cnblogs_post_body" class="blogpost-body">

问题

一般我们在Django程序中查询数据库操作都是在QuerySet里进行进行,例如下面代码:

>> q1 = Entry.objects.filter(headline__startswith=>> q2 = q1.exclude(pub_date__gte=datetime.date.today())
>> q3 = q1.filter(pub_date__gte=datetime.date.today())

或者将其组合起来,例如:

>>q1 = Entry.objects.filter(headline_startswith="What").exclude(pub_date_gte=datetime.date.today())

随着我们的程序越来越复杂,查询的条件也跟着复杂起来,这样简单的通过一个filter()来进行查询的条件将导致我们的查询越来越长。

Q()对象就是为了将这些条件组合起来。

当我们在查询的条件中需要组合条件时(例如两个条件“且”或者“或”)时。我们可以使用Q()查询对象。例如下面的代码

这样就生成一个Q()对象,我们可以函数自动生成一个新的Q()。例如下面代码就将两个条件组合成了一个

使用上述代码可以使用sql语句这么理解:

我们可以在,例如下面代码:

对应sql语句可以理解为:

这样我们查询逻辑。

也可以传递多个Q()对象给查询函数,例如下面代码:

间的关系Django会自动理解成“且(and)”关系。如上面代码使用sql语句理解将会是:

sql">

Q()对象可以结合关键字参数一起传递给查询函数,不过需要注意的是要将Q()对象放在关键字参数的前面,看下面代码

<span class="preprocessor">#错误的做法,代码关键字参数放在了Q()对象的前面。
News.objects.<span class="keyword">get(
question__startswith=<span class="string">'Who',<span class="number">6)))

查询

teral">方法中的关键字参数查询都是一起进行“AND” 的。 查询(例如teral"> 语句),你可以使用teral">

teral"> (teral">) 对象用于封装一组关键字参数关键字参数就是上文“字段查询” 中所提及的那些。

teral"> 对象封装一个teral"> 查询

teral"> 对象可以使用teral">teral"> 操作符组合起来。一个操作符在两个teral"> 对象上使用时,它产生一个新的teral"> 对象。

一个teral"> 对象,表示两个teral"> 查询的“OR” :

sql teral"> 子句:

teral">teral"> 操作符以及使用括号进行分组来编写任意复杂的teral"> 对象。teral"> 对象可以使用teral"> 操作符取反,这允许组合正常的查询和取反(teral">) 查询

关键字参数的查询函数(例如teral">teral">teral">)都可以传递一个或多个teral"> 对象作为位置(不带名的)参数。一个查询函数有多个teral"> 对象参数,这些参数的逻辑关系为“AND"。

sql:

查询函数可以混合使用teral">关键字参数查询函数的参数(关键字参数teral"> 对象)都将"AND”在一起。teral"> 对象,它必须位于所有关键字参数的前面。

一个合法的查询,等同于前面的例子;

irst admonition-title">

查询示例演示了几种teral">用法

imsun;">Django的Q对象实现的源码中:

ottom: 0px; padding-top: 0px; padding-bottom: 0px; color: #454545; font-family: 'PingFang SC',Simsun; font-size: 16px;">

ottom: 0px; padding-top: 0px; padding-bottom: 0px; color: #454545; font-family: 'PingFang SC',Simsun; font-size: 16px;">

Highlighter bg_python" style="padding: 1px 0px 0px; font-family: Consolas,'Courier New',Courier,mono,serif; font-size: 12px; background-color: #e7e5dc; width: 772.188px; overflow-x: auto; overflow-y: hidden; position: relative; color: #454545; margin: 18px 0px!important;">
normal; font-family: Verdana,Geneva,Helvetica,sans-serif; color: silver; background-color: #f8f8f8; border-left: 3px solid #6ce26c; border-right: 1px solid #e7e5dc;">[python]
    ottom: none; border-left: none; background-color: #ffffff; color: #5c5c5c; margin: 0px 0px 1px 45px!important;" start="1">
  1. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  2. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  3. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  4. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  5. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  6. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  7. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  8. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  9. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  10. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  11. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  12. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  13. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">__init__(kwargs):@H_108_404@
  14. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">__init__(children=list(args)+kwargs.items())@H_108_404@
  15. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  16. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  17. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">isinstance(other,Q):@H_108_404@
  18. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  19. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  20. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  21. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  22. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  23. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  24. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  25. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  26. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  27. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  28. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  29. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  30. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  31. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  32. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  33. ottom: none; border-left: 3px solid #6ce26c; color: inherit; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@
  34. ottom: none; border-left: 3px solid #6ce26c; background-color: #f8f8f8; line-height: 18px; margin-top: 0px!important; margin-right: 0px!important; margin-bottom: 0px!important; padding: 0px 3px 0px 10px!important;">@H_108_404@



<p class="last">


<h3 id="传q对象构造搜索条件" style="margin: 10px 0px; padding: 0px; font-size: 16px; line-height: 1.5; font-family: Verdana,sans-serif;">传Q对象,构造搜索条件

<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: #333333; font-family: Verdana,sans-serif; font-size: 13.3333px;">
首先还是需要导入模块:

display: block; height: auto; overflow-x: auto; color: #000000; padding: 5px!important; line-height: 1.5!important; font-family: 'Courier New',sans-serif!important; font-size: 12px!important; border: 1px solid #cccccc!important;">from django.db.models  Q

查询" style="margin: 10px 0px; padding: 0px; font-size: 14px; font-family: Verdana,sans-serif;">传入条件进行查询:

display: block; height: auto; overflow-x: auto; color: #000000; padding: 5px!important; line-height: 1.5!important; font-family: 'Courier New',sans-serif!important; font-size: 12px!important; border: 1px solid #cccccc!important;">q1 = Q()
q1.connector = 
q1.children.((,))
q1.children.((,))
q1.children.((,))

models.Tb1.objects.filter(q1)


<h4 id="合并条件进行查询" style="margin: 10px 0px; padding: 0px; font-size: 14px; font-family: Verdana,sans-serif;">合并条件进行查询:

display: block; height: auto; overflow-x: auto; color: #000000; padding: 5px!important; line-height: 1.5!important; font-family: 'Courier New',sans-serif!important; font-size: 12px!important; border: 1px solid #cccccc!important;">con = Q()

q1 = Q()
q1.connector = <span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'OR'
q1.children.<span class="hljs-built_in" style="margin: 0px; padding: 0px; color: #0000ff;">append((<span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'id',<span class="hljs-number" style="margin: 0px; padding: 0px;">3))

q2 = Q()
q2.connector = <span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'OR'
q2.children.<span class="hljs-built_in" style="margin: 0px; padding: 0px; color: #0000ff;">append((<span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'status',<span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'在线'))

con.add(q1,<span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'AND')
con.add(q2,<span class="hljs-string" style="margin: 0px; padding: 0px; color: #a31515;">'AND')

models.Tb1.objects.filter(con)




<p class="last">

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐