zoukankan      html  css  js  c++  java
  • Django Q对象

    使用Q 对象进行复杂的查询

    filter() 等方法中的关键字参数查询都是一起进行“AND” 的。 如果你需要执行更复杂的查询(例如OR 语句),你可以使用对象

    对象 (django.db.models.Q) 对象用于封装一组关键字参数。这些关键字参数就是上文“字段查询” 中所提及的那些。

    例如,下面的Q 对象封装一个LIKE 查询:

    from django.db.models import Q
    Q(question__startswith='What')
    

    Q 对象可以使用& 和| 操作符组合起来。当一个操作符在两个Q 对象上使用时,它产生一个新的Q 对象。

    例如,下面的语句产生一个Q 对象,表示两个"question__startswith" 查询的“OR” :

    Q(question__startswith='Who') | Q(question__startswith='What')
    

    它等同于下面的SQL WHERE 子句:

    WHERE question LIKE 'Who%' OR question LIKE 'What%'
    

    你可以组合& 和|  操作符以及使用括号进行分组来编写任意复杂的Q 对象。同时,Q 对象可以使用~ 操作符取反,这允许组合正常的查询和取反(NOT) 查询:

    Q(question__startswith='Who') | ~Q(pub_date__year=2005)
    

    每个接受关键字参数的查询函数(例如filter()exclude()get())都可以传递一个或多个Q 对象作为位置(不带名的)参数。如果一个查询函数有多个Q 对象参数,这些参数的逻辑关系为“AND"。例如:

    Poll.objects.get(
        Q(question__startswith='Who'),
        Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))
    )
    

    ... 大体上可以翻译成这个SQL:

    SELECT * from polls WHERE question LIKE 'Who%'
        AND (pub_date = '2005-05-02' OR pub_date = '2005-05-06')
    

    查询函数可以混合使用Q 对象和关键字参数。所有提供给查询函数的参数(关键字参数或Q 对象)都将"AND”在一起。但是,如果出现Q 对象,它必须位于所有关键字参数的前面。例如:

    Poll.objects.get(
        Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)),
        question__startswith='Who')
    

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

    # INVALID QUERY
    Poll.objects.get(
        question__startswith='Who',
        Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)))
    

    ... 是不合法的。

    另见

    Django 单元测试中的OR 查询示例演示了几种Q 的用法。

    Django的Q对象实现的源码中:

    [python] view plain copy
     
    1. # 位于/django/db/models/query_utils.py  
    2.   
    3. class Q(tree.Node):  
    4.     """ 
    5.     Encapsulates filters as objects that can then be combined logically (using 
    6.     & and |). 
    7.     """  
    8.     # Connection types  
    9.     AND = 'AND'  
    10.     OR = 'OR'  
    11.     default = AND  
    12.   
    13.     def __init__(self, *args, **kwargs):  
    14.         super(Q, self).__init__(children=list(args) + kwargs.items())  
    15.   
    16.     def _combine(self, other, conn):  
    17.         if not isinstance(other, Q):  
    18.             raise TypeError(other)  
    19.         obj = type(self)()  
    20.         obj.add(self, conn)  
    21.         obj.add(other, conn)  
    22.         return obj  
    23.   
    24.     def __or__(self, other):  
    25.         return self._combine(other, self.OR)  
    26.   
    27.     def __and__(self, other):  
    28.         return self._combine(other, self.AND)  
    29.   
    30.     def __invert__(self):  
    31.         obj = type(self)()  
    32.         obj.add(self, self.AND)  
    33.         obj.negate()  
    34.         return obj  

     

    传Q对象,构造搜索条件

    首先还是需要导入模块:

    from django.db.models import Q

    传入条件进行查询:

    q1 = Q()
    q1.connector = 'OR'
    q1.children.append(('id', 1))
    q1.children.append(('id', 2))
    q1.children.append(('id', 3))
        
    models.Tb1.objects.filter(q1)

    合并条件进行查询:

    con = Q()
    
    q1 = Q()
    q1.connector = 'OR'
    q1.children.append(('id', 1))
    q1.children.append(('id', 2))
    q1.children.append(('id', 3))
    
    q2 = Q()
    q2.connector = 'OR'
    q2.children.append(('status', '在线'))
    
    con.add(q1, 'AND')
    con.add(q2, 'AND')
    
    models.Tb1.objects.filter(con)
  • 相关阅读:
    python 冒泡排序
    数组
    git用法
    Leetcode 344. 反转字符串
    Leetcode 83. 删除排序链表中的重复元素
    【第一部分】01Leetcode刷题
    【C++ Primer | 07】常用算法
    【第一部分】10Leetcode刷题
    【第一部分】09Leetcode刷题
    【第一部分】07Leetcode刷题
  • 原文地址:https://www.cnblogs.com/qunxiadexiaoxiangjiao/p/8982498.html
Copyright © 2011-2022 走看看