Пример #1
0
    def exclude(self, *values, **filters):
        clone = self._clone()

        raw = []
        args = []
        for v in values:
            if isinstance(v, Q):
                args.append(v)
            else:
                raw.append(v)

        # One or more values allowed values for the applied to this
        # data field. This is currently restricted to an `exact` or `in`
        # operator.
        if raw:
            if len(raw) == 1:
                condition = clone.field_name, raw[0]
            else:
                condition = u'{0}__in'.format(clone.field_name), raw
            clone._exclude.append(M(tree=clone.model, **dict([condition])))

        clone._exclude.extend(args)

        # Separate out the conditions that apply to aggregations. The
        # non-aggregation conditions will always be applied before the
        # aggregation is applied.
        for key, value in filters.iteritems():
            if key.split(LOOKUP_SEP)[0] in clone._aggregates:
                condition = ~Q(**dict([(key, value)]))
                clone._having.append(condition)
            else:
                condition = ~M(tree=clone.model, **dict([(key, value)]))
                clone._exclude.append(condition)
        return clone
Пример #2
0
    def test_variations(self):
        tests = [
            # basic, no operator
            (M(office__location='Outer Space'),
             "(AND: ('office__location', 'Outer Space'))"),
            # full, no operator
            (M(tests__office__location='Outer Space'),
             "(AND: ('office__location', 'Outer Space'))"),
            # full, operator
            (M(tests__office__location__iexact='Outer Space'),
             "(AND: ('office__location__iexact', 'Outer Space'))"),

            # alternate root, basic, no operator
            (M('project', title__salary=100000),
             "(AND: ('employees__title__salary', 100000))"),
            # alternate root, basic, operator
            (M('project', title__salary__gt=100000),
             "(AND: ('employees__title__salary__gt', 100000))"),
            # alternate root, full, no operator
            (M('project', tests__title__salary=100000),
             "(AND: ('employees__title__salary', 100000))"),

            # complex
            (M('project', title__salary=100000)
             & M(office__location='Outer Space'),
             "(AND: ('employees__title__salary', 100000), "
             "('office__location', 'Outer Space'))"),
            (M('project', title__salary=100000)
             | M(office__location='Outer Space'),
             "(OR: ('employees__title__salary', 100000), "
             "('office__location', 'Outer Space'))"),
        ]

        for m, s in tests:
            self.assertEqual(str(m), s)
Пример #3
0
 def _filter_or_exclude(self, negate, *args, **kwargs):
     return super(ModelTreeQuerySet, self)\
         ._filter_or_exclude(negate, M(self.tree, *args, **kwargs))