Hello Bern,

You're actually pretty close to the solution.

You should be able to OR an existing querysets filters by using the "|" 
operator
between querysets of the same model. That would be the following:

qs |= Model.objects.filter(Q(...))

Simon

Le mercredi 14 mars 2018 23:45:41 UTC-4, Bernd Wechner a écrit :
>
> Another interesting problem.
>
> Let's say I have a QuerySet which already applies some filters. It's 
> provided by a third party library mind you, and all you have is the 
> QuerySet instance. 
>
> Now I would like to loosen the filter a bit, ad an ORed filter. Not a 
> trivial thing it seems. Q objects aren't obviously available unless we can 
> extract the Q object for the existing filter.
>
> In code the scenario looks like:
>
> # A Queryset is buillt with some unknown filter criteria
> qs = Model.objects.filter(....)
> # All we see in out code is the QuerySet qs. But we want somehow to add a 
> filter criterion. We can do this:
> qs = qs.filter(...) 
> # and the new filter criteria will be added, but with AND logic. What if 
> we want to add one with OR logic (loosen the filter some)
> # It might look like:
> qs = qs | Q(...)
> # But does it? How can we add an ORed filter criterion to qs?
>
> Basically, there's a chance it's not possible! But if it is, it would rely 
> on some syntax for combining chained .filter() methods with an OR logic 
> (which seems constrained to Q objects) or extracting the current filter 
> from a QuerySet and its constraints as a Q object, so we can OR a new 
> constraint to it, and then reapply the filter.
>
> Bit of a mystery to me. 
>
> Hoping someone has some insights I'm not getting from the docs here.
>
> Regards,
>
> Bernd.
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d9d845e5-62c2-479c-83cc-6d21b719a74f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to