Ticket #6074: 6074.diff

File 6074.diff, 2.3 KB (added by Honza Král, 12 years ago)

my take on the patch

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 4d0d295..df9018b 100644
    a b class QOperator(object): 
    715715    def get_sql(self, opts):
    716716        joins, where, params = SortedDict(), [], []
    717717        for val in self.args:
    718             try:
    719                 joins2, where2, params2 = val.get_sql(opts)
    720                 joins.update(joins2)
    721                 where.extend(where2)
    722                 params.extend(params2)
    723             except EmptyResultSet:
    724                 if not isinstance(self, QOr):
    725                     raise EmptyResultSet
     718            joins2, where2, params2 = val.get_sql(opts)
     719            joins.update(joins2)
     720            where.extend(where2)
     721            params.extend(params2)
    726722        if where:
    727723            return joins, ['(%s)' % self.operator.join(where)], params
    728724        return joins, [], params
    class QAnd(QOperator): 
    743739
    744740class QOr(QOperator):
    745741    "Encapsulates a combined query that uses 'OR'."
     742    def get_sql(self, opts):
     743        joins, where, params = SortedDict(), [], []
     744        failed = 0
     745        for val in self.args:
     746            try:
     747                joins2, where2, params2 = val.get_sql(opts)
     748                joins.update(joins2)
     749                where.extend(where2)
     750                params.extend(params2)
     751            except EmptyResultSet:
     752                failed += 1
     753        if failed == len( self.args ):
     754            raise EmptyResultSet
     755
     756        if where:
     757            return joins, ['(%s)' % self.operator.join(where)], params
     758        return joins, [], params
    746759    operator = ' OR '
    747760    def __and__(self, other):
    748761        return QAnd(self, other)
  • tests/modeltests/or_lookups/models.py

    diff --git a/tests/modeltests/or_lookups/models.py b/tests/modeltests/or_lookups/models.py
    index ee2cfd2..a529b0f 100644
    a b __test__ = {'API_TESTS':""" 
    116116[<Article: Hello>]
    117117>>> Article.objects.complex_filter(Q(pk=1) | Q(pk=2))
    118118[<Article: Hello>, <Article: Goodbye>]
     119
     120# Check that Q(id__in=[]) returns the same results as Q(id__in=[]) | Q(id__in=[]) when used with filter :
     121>>> Article.objects.filter(Q(id__in=[]) | Q(id__in=[]))
     122[]
     123>>> Article.objects.filter(Q(id__in=[]) | Q(id=1) |Q(id__in=[]))
     124[<Article: Hello>]
    119125"""}
Back to Top