Opened 5 years ago

Closed 22 months ago

Last modified 16 months ago

#20939 closed Cleanup/optimization (fixed)

Convert QuerySet to Query when filtering

Reported by: Anssi Kääriäinen Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The current ORM coding has very similar, yet not equivalent paths for using QuerySet and the internal Query when doing filter(something=queryset_or_query). It seems that converting a QuerySet to Query early on would lead to cleaner implementation. Proof of concept here:

There are two problems in the current implementation:

  • The way user-defined .values() and Django issued .values() in build_filter() is detected is ugly
  • There is no multidb protection (test_subquery in multiple_database.tests.QueryTestCase) fails).

I'll mark this as accepted directly in the sense of "worth doing if above problems can be addressed).

Change History (6)

comment:1 Changed 23 months ago by Tim Graham

I rebased and updated this to see if it's still worth doing and to learn something in the process. It's still incomplete and tests aren't passing: PR.

comment:2 Changed 22 months ago by GitHub <noreply@…>

Resolution: fixed
Status: newclosed

In 1bc249c:

Fixed #20939 -- Simplified query generation by converting QuerySet to Query.

Thanks Anssi Kääriäinen for the initial patch and Anssi, Simon Charette,
and Josh Smeaton for review.

comment:3 Changed 16 months ago by Simon Charette <charette.s@…>

In ec50937:

Refs #20939 -- Removed the Query._forced_pk hack.

comment:4 Changed 16 months ago by Simon Charette <charette.s@…>

In c9159a08:

Refs #20939 -- Removed the Query._prepare_as_filter_value() hack.

comment:5 Changed 16 months ago by Simon Charette <charette.s@…>

In e62ea0bb:

Refs #20939 -- Moved subquery ordering clearing optimization to the in lookup.

Queries could potentially be resolved in cases where ordering matter.

comment:6 Changed 16 months ago by Tim Graham <timograham@…>

In eb4724a0:

Reverted "Refs #20939 -- Moved subquery ordering clearing optimization to the in lookup."

This reverts commit e62ea0bb9cbb54c1eef848871fe3eab2bad268dc since it
broke multi-column in lookups and _meta.order_wrt on Oracle.

Note: See TracTickets for help on using tickets.
Back to Top