@register.assignment_tag
def exclude_queryset_by(queryset, **excludes):

    if not getattr(queryset, 'query', False):
        return queryset

    cachekey = u'excludequerysetby-{0}'.format(hash(unicode(queryset.query)))
    _cache = cache.get(cachekey)
    if _cache:
        return _cache

    # check for __in lookups and split the param
    found_in_lookup = None
    for key in excludes.keys():
        if '__in' in key:
            found_in_lookup = key

    if found_in_lookup:
        excludes[found_in_lookup] = excludes[found_in_lookup].split(',')

    if not queryset.query.can_filter():
        # create new queryset based on the ids and apply filter
        ids = [i.id for i in queryset]
        containers = queryset.model.objects.filter(id__in=ids).exclude(
            **excludes
        )
    else:
        containers = queryset.exclude(**excludes)

    if 'child_class' in excludes:
        # we need to exclude the mirrors containing the child_class that
        # we want to exclude
        bad_child_class = excludes['child_class']
        mirrors = Mirror.objects.filter(
            container__child_class=bad_child_class
        ).values_list('id', flat=True)
        if mirrors:
            containers = containers.exclude(pk__in=mirrors)

    cache.set(cachekey, containers, settings.OPPS_CACHE_EXPIRE)
    return containers
Exclude object list
dataset/ETHPy150Open opps/opps/opps/containers/templatetags/container_tags.py/exclude_queryset_by