django icon indicating copy to clipboard operation
django copied to clipboard

Fixed #36385 -- Simplified filtering in QuerySet.

Open ngnpope opened this issue 6 months ago • 2 comments

Trac ticket number

ticket-36385

Branch description

Some of the code for filtering QuerySet is overly complex which makes it hard to follow:

  • Methods that are nearly identical, e.g. QuerySet._clone() vs QuerySet._chain() and Query.clone() vs Query.chain()
  • Private API like Queryset.complex_filter() which exists for a specific case but can be handled using public APIs
  • Flags such as _defer_next_filter and _sticky_filter that affect the next call to QuerySet.filter()
  • Passing around deconstructed pieces instead of building a Q() as early as possible
  • Not using the klass argument to Query.chain()

I had hoped to eliminate Query.filter_is_sticky and pass the used_aliases to Query.add_q(), but I think that's a non-starter as it has to apply after the next clone. (Hard to get my head around it!)

Checklist

  • [x] This PR targets the main branch.
  • [x] The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • [x] I have checked the "Has patch" ticket flag in the Trac system.
  • [x] I have added or updated relevant tests.
  • [x] I have added or updated relevant docs, including release notes if applicable.
  • [x] I have attached screenshots in both light and dark modes for any UI changes.

ngnpope avatar Apr 30 '25 19:04 ngnpope