[Django] #26108: New method for returning OR querysets

18 views
Skip to first unread message

Django

unread,
Jan 20, 2016, 5:46:56 PM1/20/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
----------------------------------------------+--------------------
Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
It would be nice if you guys created a new method that would allow for OR
querysets to be returned.

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.

Django

unread,
Jan 20, 2016, 5:56:03 PM1/20/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------

Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* 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>

Django

unread,
Jan 20, 2016, 6:28:19 PM1/20/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------

Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 21, 2016, 6:23:00 PM1/21/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------

Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* cc: akaariai (added)


Comment:

Anssi, any comments?

--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:3>

Django

unread,
Jan 22, 2016, 1:03:02 AM1/22/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------

Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 22, 2016, 12:30:35 PM1/22/16
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------

Reporter: MrMe99 | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Feb 10, 2017, 7:26:42 PM2/10/17
to django-...@googlegroups.com
#26108: New method for returning OR querysets
-------------------------------------+-------------------------------------
Reporter: Andy Harb | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

Closing in absence of further discussion.

--
Ticket URL: <https://code.djangoproject.com/ticket/26108#comment:6>

Reply all
Reply to author
Forward
0 new messages