Ticket #17600: exclude.r17463.diff

File exclude.r17463.diff, 1.4 KB (added by pmartin, 3 years ago)

Add the patche

  • db/models/query.py

     
    631631        if args or kwargs:
    632632            assert self.query.can_filter(), \
    633633                    "Cannot filter a query once a slice has been taken."
    634 
    635634        clone = self._clone()
     635        lookup = Q(*args, **kwargs)
    636636        if negate:
    637             clone.query.add_q(~Q(*args, **kwargs))
    638         else:
    639             clone.query.add_q(Q(*args, **kwargs))
     637            lookup.negate_last_node()
     638        clone.query.add_q(lookup)
    640639        return clone
    641640
    642641    def complex_filter(self, filter_obj):
  • db/models/query_utils.py

     
    4343    def __init__(self, *args, **kwargs):
    4444        super(Q, self).__init__(children=list(args) + kwargs.items())
    4545
     46    def negate_last_node(self):
     47        if not self.children:
     48            return
     49        for child in self.children:
     50            if not isinstance(child, tree.Node):
     51                self.negated = True
     52            else:
     53                child.negate_last_node()
     54
    4655    def _combine(self, other, conn):
    4756        if not isinstance(other, Q):
    4857            raise TypeError(other)
Back to Top