[Django] #36385: Simplify implementation of filtering in QuerySet.

13 views
Skip to first unread message

Django

unread,
May 12, 2025, 8:03:18 AM5/12/25
to django-...@googlegroups.com
#36385: Simplify implementation of filtering in QuerySet.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: dev
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 |
-------------------------------------+-------------------------------------
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()`
- These used to do more, but now are not significantly different such
that they're worth keeping separate.
- 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()` object
as early as possible
- Not using the `klass` argument to `Query.chain()` and altering
`__class__` manually

We should be able to simplify this, removing a number of flag attributes
and methods in the process.
--
Ticket URL: <https://code.djangoproject.com/ticket/36385>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 12, 2025, 8:06:16 AM5/12/25
to django-...@googlegroups.com
#36385: Simplify implementation of filtering in QuerySet.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/19438 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36385#comment:1>

Django

unread,
May 12, 2025, 10:53:10 AM5/12/25
to django-...@googlegroups.com
#36385: Simplify implementation of filtering in QuerySet.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* cc: Simon Charette (added)
* stage: Unreviewed => Accepted

Comment:

Hello Nick, thank you for the ticket and carefully crafted PR. After a
quick look this seems like a positive cleanup, so I'm accepting pending
feedback from Simon in the PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/36385#comment:2>

Django

unread,
May 26, 2025, 4:42:08 PM5/26/25
to django-...@googlegroups.com
#36385: Simplify implementation of filtering in QuerySet.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1

Comment:

The patch itself is great and a very welcomed cleanup but I suggest we
defer the complete removal of the internal methods with deprecation shims
and mentions in the release notes as it will inevitably break code out
there.

Marking as needing improvement for this reason.
--
Ticket URL: <https://code.djangoproject.com/ticket/36385#comment:3>
Reply all
Reply to author
Forward
0 new messages