Django: remove a filter condition from a queryset?

Up vote 6 down vote favorite 1 share g+ share fb share tw.

I have a third-part funtion which gives me a filtered queryset (e.g. Records with 'valid'=True) but I want to remove a particular condition (e.g. To have all records, both valid and invalid). Is there a way to remove a filter condition to an already-filtered queryset? E.g.

Only_valid = MyModel.objects. Filter(valid=True) all_records = only_valid. **remove_filter**('valid') (I know that it would be better to define 'all_records' before 'only_valid', but this is just an example...) django django-models filtering django-queryset link|improve this question edited Jan 14 '11 at 10:47 asked Jan 14 '11 at 9:20Don2,350111 70% accept rate.

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.

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.

This is a good hint, thanks. – Don Jan 14 '11 at 11:28 You will need access to the third party code, but yes, that's a good solution. +1 – Boldewyn Jan 14 '11 at 11:30.

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_reccords ... all_records = only_valid. Original Obviously this clears any other filters too so it won't be right for every case.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions