[Django]-Django: remove a filter condition from a queryset

7👍

From the docs:

Each time you refine a QuerySet, you get a brand-new QuerySet that is in no way bound to the previous QuerySet. Each refinement creates a separate and distinct QuerySet that can be stored, used and reused.

I doubt therefore, that there is a standard way to do it. You could dig into the code, see, what filter() does and try a bit. If that doesn’t help, my assumption is, you’re out of luck and need to re-build the query yourself.

18👍

Although there is no official way to do this using filter notation, you may easily do it with Q-notation.
For example, if you ensure that third-part function returns a Q object, not a filtered QuerySet, you may do the following:

q = ThirdParty()
q = q | Q(valid=False)

And the resulting SQL conditions will be joined using OR operator.

6👍

Use this function

from django.db.models import Q

def remove_filter(lookup, queryset):
    """
    Remove filter lookup in queryset
    ```
    >>> queryset = User.objects.filter(email='user@gmail.com')
    >>> queryset.count()
    1
    >>> remove_filter('email', queryset)
    >>> queryset.count()
    1000
    ```
    """
    query = queryset.query
    q = Q(**{lookup: None})
    clause, _ = query._add_q(q, self.used_aliases)

    def filter_lookups(child):
        return child.lhs.target != clause.children[0].lhs.target

    query.where.children = list(filter(filter_lookups, query.where.children))

1👍

Here’s what I did in a similar case.

all_records = MyModel.objects.all()
only_valid = MyModel.objects.filter(valid=True)
only_valid.original = all_records

...

all_records = only_valid.original

Obviously this clears any other filters too so it won’t be right for every case.

👤Jake

0👍

original_query_set = MyModel.objects.filter(**conditions)
model_class = orginal_query_set.model
new_query_set = model_class.objects.filter(**new_conditions)

You can use the .model attribute on a QuerySet to get the model class, then use the model class to make a brand new QuerySet.

-1👍

Thanks for making me check the source code Boldewyn. So, it seems there’s a new method right below filter() with the same parameters… called exclude() (as of commit mini-hash ef6c680, for when it loses its line number)

Return a new QuerySet instance with NOT (args) ANDed to the existing set.

So, to answer the original question:

only_valid = MyModel.objects.filter(valid=True)
filtered_results = only_valid.exclude(the_condition_to_remove=True)
👤iddy

Leave a comment