[Django] #31397: Support qs.filter(related=Q(...))

5 views
Skip to first unread message

Django

unread,
Mar 24, 2020, 1:06:39 PM3/24/20
to django-...@googlegroups.com
#31397: Support qs.filter(related=Q(...))
-------------------------------------+-------------------------------------
Reporter: Roman | Owner: nobody
Odaisky |
Type: New | Status: new
feature |
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
It would be very helpful to be able to use Q objects with related models,
especially if some other code generates the Q objects. Use case:

{{{
def bestsellers(q):
qs = Author.objects.filter(q).annotate(total_sold=Sum("book__n_sold"))
...
}}}
Suppose that later the calculations become so complex that they can’t be
expressed in terms of aggregate functions, thus Python code has to be
written instead, and the queryset changes models:
{{{
def bestsellers(q): # q still contains fields from the Author model
qs = Book.objects.filter(???)
for book in qs:
...
}}}
It would be natural to try to write `Book.objects.filter(author=q)`, but
this syntax isn’t supported.

I suggest that `Q(model=Q(a=b, c=d))` be allowed with the same effect as
`Q(model__a=b, model__c=d)` (recursively if the inner Q is nested).

The implementation does not seem to be particularly complex. This function
does most of the work:
{{{
def prefix_q(prefix, q):
""" Q(prefix=q) === prefix_q(prefix, q) """
if isinstance(q, tuple):
return (prefix + "__" + q[0], q[1])

return Q(*[prefix_q(prefix, c) for c in q.children],
_connector=q.connector, _negated=q.negated)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31397>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 25, 2020, 1:07:39 AM3/25/20
to django-...@googlegroups.com
#31397: Support qs.filter(related=Q(...)).
-------------------------------------+-------------------------------------
Reporter: Roman Odaisky | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
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 felixxm):

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


Comment:

Duplicate of #16979.

--
Ticket URL: <https://code.djangoproject.com/ticket/31397#comment:1>

Reply all
Reply to author
Forward
0 new messages