Skip to content

Add opt-in grouping for ORM filters - #324

Open
UmutAlisoglu wants to merge 1 commit into
SAP:masterfrom
UmutAlisoglu:codex/opt-in-filter-grouping
Open

UmutAlisoglu wants to merge 1 commit into
SAP:masterfrom
UmutAlisoglu:codex/opt-in-filter-grouping

Conversation

@UmutAlisoglu

@UmutAlisoglu UmutAlisoglu commented Oct 26, 2026 •

Copy link
Copy Markdown

Mixing an ORM __in lookup or a Q expression containing or with other filters can select rows that fail the other conditions. For example, ID__in=[2, 3], NameLast='Smith' currently allows ID 2 regardless of its name.

This implements the opt-in group=True approach suggested in #280. It groups individual conditions, Q arguments and the preceding filter on chained calls. Grouping stays disabled by default, and all existing query serialization tests pass. The new option reserves group on .filter(); an entity property with that name remains accessible through Q(group=value).

The 14 new cases cover mixed keyword/Q conditions, __in inside Q, compound OR, AND, chained calls, string replacement, empty input, unchanged legacy output and Unicode encoding. Parameterized cases assert explicit filter strings and fully encoded URLs from mocked HTTP requests, alongside evaluating the selected rows with Python's independent AST parser.

Documentation and the Unreleased changelog are updated. Thanks to @developer992 for reporting the problem and @phanak-sap for the opt-in design. Addresses #280; follows up on the closed default-changing proposals #283 and #321.

Validation on Windows / Python 3.11.9:

  • Full suite after rebasing onto current master: 314 passed, 93% overall coverage.
  • Required Pylint 2.15.0: 10.00/10; required Flake8 3.8.4 passes.
  • Sphinx generates HTML, but -W fails on the same five pre-existing configuration/vendor-page warnings on both original and patched documentation.
  • git diff --check passes.

AI-assisted with OpenAI Codex, including investigation, implementation and verification.

@cla-assistant

cla-assistant Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@phanak-sap phanak-sap added the enhancement New feature or request label Oct 26, 2026
@phanak-sap

Copy link
Copy Markdown
Contributor

matching a hardcoded URL

Hi @UmutAlisoglu thx for the PR (and explicit note about AI usage). This looks like solid enhancement (yeah, "opt-in" not backward breaking, as envisioned in #280).

I understand your/agent logic for ast module in test. Yeah, makes sense and will stay. But for readability reasons and perhaps even possible encoding problems, the parameterized tests could actually also have the hardcoded expected URLs - not as "rather than only" but as "addition to ast".

@UmutAlisoglu

Copy link
Copy Markdown
Author

Thanks. I kept the AST checks and added explicit expected filter strings and full encoded URLs to each parameterized case. The tests also execute mocked requests and check the actual URL, including a Unicode case. All 312 tests pass locally.

@phanak-sap

Copy link
Copy Markdown
Contributor

Thanks. I kept the AST checks and added explicit expected filter strings and full encoded URLs to each parameterized case. The tests also execute mocked requests and check the actual URL, including a Unicode case. All 312 tests pass locally.

thx. pls resolve the conflict around changelog from different PR merged. funny, around similar area.
I will go trough review approval but looking really good.

phanak-sap
phanak-sap previously approved these changes Oct 26, 2026

@phanak-sap phanak-sap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jumping between PRs that appeared past two days I have read through this several time. I do not see any apparent problem, I like that the default is False so no breaking changes for current users, docs and test are OK.

Checked with AI tools as well.

@phanak-sap

Copy link
Copy Markdown
Contributor

I have a itch/gut feeling that more complex test cases for multiple AND/OR/Parenthesis could be added, but that can be safely part of separate PR anyway, due hiding essentially behind a feature flag.

Group field lookups, Q arguments and preceding chained filters only when group=True. Retain default query serialization and test selected rows with an independent boolean parser. Follow the approach suggested by phanak-sap in SAP#280, originally reported by developer992.
@UmutAlisoglu

Copy link
Copy Markdown
Author

Rebased onto current master and kept both changelog entries. All 314 tests and the required Pylint/Flake8 checks pass locally. The conflict is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants