Code

Ticket #16211: django-query-expression-negate.patch

File django-query-expression-negate.patch, 1.4 KB (added by wdoekes, 3 years ago)
  • django/db/models/expressions.py

    old new class ExpressionNode(tree.Node): 
    2020    AND = '&' 
    2121    OR = '|' 
    2222 
     23    # Logical operators 
     24    NOT = 'NOT' # unary, needs special attention in combine_expression 
     25 
    2326    def __init__(self, children=None, connector=None, negated=False): 
    2427        if children is not None and len(children) > 1 and connector is None: 
    2528            raise TypeError('You have to specify a connector.') 
    class ExpressionNode(tree.Node): 
    4851    # OPERATORS # 
    4952    ############# 
    5053 
     54    def __invert__(self): 
     55        obj = ExpressionNode([self], connector=self.NOT, negated=True) 
     56        return obj 
     57 
    5158    def __add__(self, other): 
    5259        return self._combine(other, self.ADD, False) 
    5360 
  • django/db/backends/__init__.py

    old new class BaseDatabaseOperations(object): 
    472472        can vary between backends (e.g., Oracle with %% and &) and between 
    473473        subexpression types (e.g., date expressions) 
    474474        """ 
     475        if connector == 'NOT': 
     476            assert len(sub_expressions) == 1 
     477            return 'NOT (%s)' % sub_expressions[0] 
    475478        conn = ' %s ' % connector 
    476479        return conn.join(sub_expressions) 
    477480