Right now you can use <model>.objects.filter() in conjunction with the q()
method but this is limiting since you have to have all the ORs in a single
filter statement. If you where to chain another filter statement the two
would be ANDed.
It would be helpful to have another method that would OR the chained
results together. This is helpful when you have conditional cases and
need to build the queryset up.
Example:
Say i have an object I'm querying and the query looks like this so far:
query = <model>.objects.filter( (Q(id=2) & Q(state=1)) | (Q(id=3) &
(state = 2)) ... )
Now lets say I want to append another OR to the end of the query if the
current logged in user is and admin. Currently I would have to write a
conditional statement and duplicate the query in two places except the
query with the if user.is_admin would have some additional conditions.
It would be ideal if we could just append another statement with an OR to
the current query:
example : query.include( Q(id=8) & Q(state=3))
This would allow greater flexibility and prevent wet code.
--
Ticket URL: <https://code.djangoproject.com/ticket/26108>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Did you try combining the two querysets using the the pipe operator?
{{{#!python
Model.objects.filter(Q(id=2) & Q(state=1)) | Model.objects.filter(Q(id=8)
& Q(state=3))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:1>
Comment (by MrMe99):
Replying to [comment:1 charettes]:
> Did you try combining the two querysets using the the pipe operator?
>
> {{{#!python
> Model.objects.filter(Q(id=2) & Q(state=1)) |
Model.objects.filter(Q(id=8) & Q(state=3))
> }}}
Yes that works however it doesn't resolve the initial request. Basically
if you use ORs you have to have the entire set in a single filter method
call. If you chain another .filter to the above it will ANDed it not OR
it
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:2>
* cc: akaariai (added)
Comment:
Anssi, any comments?
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:3>
Comment (by akaariai):
This feature is somewhat straightforward to add, so I don't see much
technical problems with this.
I don't recall ever having a need for this, so for that reason a mild -0.
I guess we could ask around django-developers if there is enough support
for this.
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:4>
* stage: Unreviewed => Someday/Maybe
Comment:
Andy, could you start a thread on the DevelopersMailingList as Anssi
suggested?
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:5>
* status: new => closed
* resolution: => wontfix
Comment:
Closing in absence of further discussion.
--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:6>