'When doing an exclude against any kind of N-to-many relation, we need
to use a subquery. This method constructs the nested query, given the
original exclude filter (filter_expr) and the portion up to the first
N-to-many relation field.'

def split_exclude(self, filter_expr, prefix, can_reuse):

    query = Query(self.model)
    query.add_filter(filter_expr, can_reuse=can_reuse)
    query.bump_prefix()
    query.clear_ordering(True)
    query.set_start(prefix)
    (alias, col) = query.select[0]
    query.where.add((Constraint(alias, col, None), 'isnull', False), AND)
    self.add_filter((('%s__in' % prefix), query), negate=True, trim=True, can_reuse=can_reuse)
    active_positions = [pos for (pos, count) in enumerate(query.alias_refcount.itervalues()) if count]
    if (active_positions[(-1)] > 1):
        self.add_filter((('%s__isnull' % prefix), False), negate=True, trim=True, can_reuse=can_reuse)
