{{{
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.
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #16979.
--
Ticket URL: <https://code.djangoproject.com/ticket/31397#comment:1>