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.
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Switching to PostgreSQL fails loading datadump
- [Django]-Django-taggit – how do I display the tags related to each record
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))
- [Django]-How to add superuser in Django from fixture
- [Django]-Equivalent of PHP "echo something; exit();" with Python/Django?
- [Django]-CORS error while consuming calling REST API with React
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.
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-How to serve media files on Django production environment?
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.
- [Django]-Negating a boolean in Django template
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-Extend base.html problem
-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)
- [Django]-How do I deploy Django on AWS?
- [Django]-Django: For Loop to Iterate Form Fields
- [Django]-Django :How to integrate Django Rest framework in an existing application?