summaryrefslogtreecommitdiff
path: root/django/db/models/query_utils.py
AgeCommit message (Collapse)Author
2026-04-28Fixed #36912 -- Added connector validation to Q.create().Anna Makarudze
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
2026-04-22Fixed #37057 -- Adjusted UniqueConstraint handling of UNKNOWN condition.Simon Charette
When we adjusted UNKNOWN handling for CheckConstraint in refs #33996 we assumed that all usage of Q.check would benefit from this approach. However while CHECK constraints enforcement do ignore conditions involving NULL that resolve to UNKNOWN it's not the case for other type of constraints such as UNIQUE ones. Given how UNKNOWN should be treated depends on the callers context it appears that a better strategy for COALESCE wrapping is to force them to apply it if necessary. Thanks Drew Shapiro for the report.
2026-04-18Fixed #37036 -- Added missing flat=True arg in DeferredAttribute.fetch_many().garybadwal
2026-04-03Fixed #37016 -- Avoided propagating invalid arguments from When() to Q().varunkasyap
2025-11-05Fixed CVE-2025-64459 -- Prevented SQL injections in Q/QuerySet via the ↵Jacob Walls
_connector kwarg. Thanks cyberstan for the report, Sarah Boyce, Adam Johnson, Simon Charette, and Jake Howard for the reviews.
2025-10-16Fixed #28586 -- Added model field fetch modes.Adam Johnson
May your database queries be much reduced with minimal effort. co-authored-by: Andreas Pelme <andreas@pelme.se> co-authored-by: Simon Charette <charette.s@gmail.com> co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
2025-08-04Fixed #34871, #36518 -- Implemented unresolved lookups expression replacement.Simon Charette
This allows the proper resolving of lookups when performing constraint validation involving Q and Case objects. Thanks Andrew Roberts for the report and Sarah for the tests and review.
2025-07-23Refs #36500 -- Rewrapped long docstrings and block comments via a script.django-bot
Rewrapped long docstrings and block comments to 79 characters + newline using script from https://github.com/medmunds/autofix-w505.
2025-05-16Fixed #33312 -- Raised explicit exception when copying deferred model instances.Simon Charette
Previously save() would crash with an attempted forced update message, and both save(force_insert=True) and bulk_create() would crash with DoesNotExist errors trying to retrieve rows with an empty primary key (id IS NULL). Implementing deferred field model instance copying might be doable in certain cases (e.g. when all the deferred fields are db generated) but that's not trivial to implement in a backward compatible way. Thanks Adam Sołtysik for the report and test and Clifford for the review.
2024-09-09Refs #373 -- Added Model._is_pk_set() abstraction to check if a Model's PK ↵Csirmaz Bendegúz
is set.
2024-09-02Fixed #35712 -- Prevented Q.check() from leaving the connection in an ↵Alex Fischer
unusable state. Co-authored-by: Simon Charette <charette.s@gmail.com>
2024-05-02Refs #34007, Refs #35359 -- Added Q.referenced_based_fields property.David Sanders
Thank you to Mariusz Felisiak and Natalia Bidart for the reviews.
2024-04-23Refs #35356 -- Clarified select related with masked field logic.Simon Charette
By always including related objects in the select mask via adjusting the defer logic (_get_defer_select_mask()), it becomes possible for select_related_descend() to treat forward and reverse relationships indistinctively. This work also simplifies and adds comments to select_related_descend() to make it easier to understand.
2024-02-26Fixed #35241 -- Cached model's full parent list.Adam Johnson
co-authored-by: Keryn Knight <keryn@kerynknight.com> co-authored-by: Natalia <124304+nessita@users.noreply.github.com> co-authored-by: David Smith <smithdc@gmail.com> co-authored-by: Paolo Melchiorre <paolo@melchiorre.org>
2024-01-26Applied Black's 2024 stable style.Mariusz Felisiak
https://github.com/psf/black/releases/tag/24.1.0
2023-09-16Fixed #34842 -- Fixed ModelAdmin.readonly_fields crash with GeneratedFields.Paolo Melchiorre
2023-09-07Fixed #31300 -- Added GeneratedField model field.Jeremy Nauta
Thanks Adam Johnson and Paolo Melchiorre for reviews. Co-Authored-By: Lily Foote <code@lilyf.org> Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@gmail.com>
2023-08-23Fixed #34744 -- Prevented recreation of migration for constraints with a ↵David Sanders
dict_keys. Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
2023-04-24Refs #33766 -- Removed sql.Query.build_filtered_relation_q().Simon Charette
It was a copy of sql.Query._add_q that avoided JOIN updates.
2023-04-24Fixed #33766 -- Resolved FilteredRelation.condition at referencing time.Simon Charette
The previous implementation resolved condition at Join compilation time which required introducing a specialized expression resolving mode to alter the join reuse logic solely during that phase. FilteredRelation.condition is now resolved when the relation is first referenced which maintains the existing behavior while allowing the removal of the specialized resolving mode and address an issue where conditions couldn't spawn new joins.
2023-01-18Refs #34233 -- Used @functools.cache.Nick Pope
Python 3.9+ supports @functools.cache as an alias for @functools.lru_cache(maxsize=None).
2022-11-09Fixed #28477 -- Stripped unused annotations on aggregation.Simon Charette
Also avoid an unnecessary pushdown when aggregating over a query that doesn't have aggregate annotations.
2022-09-13Fixed #33996 -- Fixed CheckConstraint validation on NULL values.David Sanders
Bug in 667105877e6723c6985399803a364848891513cc. Thanks James Beith for the report.
2022-09-02Fixed #29799 -- Allowed registering lookups per field instances.Allen Jonathan David
Thanks Simon Charette and Mariusz Felisiak for reviews and mentoring this Google Summer of Code 2022 project.
2022-08-30Fixed #21204 -- Tracked field deferrals by field instead of models.Simon Charette
This ensures field deferral works properly when a model is involved more than once in the same query with a distinct deferral mask.
2022-08-30Merged nested if statements in select_related_descend().Simon Charette
2022-07-27Refs #32948 -- Simplified and optimized Q._combine() and __invert__().Nick Pope
- Removed use of Q.deconstruct() in Q._combine(). - Simplified and optimized Q.__invert__() by taking a shallow copy and swapping the negated attribute only. - Simplified construction in Q._combine(). - Simplified conditions in Q._combine() as Q.conditional = True the first isinstance() check is unnecessary. - Removed copy.copy() branch in Q._combine(). Co-authored-by: Keryn Knight <keryn@kerynknight.com>
2022-05-04Refs #30581 -- Added Q.check() hook.Gagaro
2022-05-03Refs #30581 -- Added Q.flatten().Gagaro
2022-04-12Fixed #33626 -- Cleared cache when unregistering a lookup.Himanshu-Balasamanta
2022-03-31Updated select_related_descend() comment.Mariusz Felisiak
Outdated since 0c7633178fa9410f102e4708cef979b873bccb76.
2022-03-04Fixed #29865 -- Added logical XOR support for Q() and querysets.Ryan Heard
2022-02-07Refs #33476 -- Reformatted code with Black.django-bot
2021-04-28Fixed #32632, Fixed #32657 -- Removed flawed support for Subquery ↵Simon Charette
deconstruction. Subquery deconstruction support required implementing complex and expensive equality rules for sql.Query objects for little benefit as the latter cannot themselves be made deconstructible to their reference to model classes. Making Expression @deconstructible and not BaseExpression allows interested parties to conform to the "expression" API even if they are not deconstructible as it's only a requirement for expressions allowed in Model fields and meta options (e.g. constraints, indexes). Thanks Phillip Cutter for the report. This also fixes a performance regression in bbf141bcdc31f1324048af9233583a523ac54c94.
2021-03-17Fixed #32548 -- Fixed crash when combining Q() objects with boolean expressions.Jonathan Richards
2021-02-18Fixed #32455 -- Allowed right combining Q() with boolean expressions.Hasan Ramezani
2021-02-18Fixed #32450 -- Fixed crash when ANDing/ORing an empty Q() with not ↵starryrbs
pickleable Q(). Regression in bb0b6e526340e638522e093765e534df4e4393d2.
2021-01-14Refs #30988 -- Removed InvalidQuery exception per deprecation timeline.Mariusz Felisiak
2020-06-10Fixed #31680 -- Removed unnecessary getattr() call in ↵alosultan
DeferredAttribute.__get__(). refresh_from_db() loads fields values.
2020-01-29Simplified DeferredAttribute.__get__() a bit.Taoup
2019-12-06Fixed #31060 -- Reallowed window expressions to be used in conditions ↵Alex Aktsipetrov
outside of queryset filters. Regression in 4edad1ddf6203326e0be4bdb105beecb0fe454c4. Thanks utapyngo for the report.
2019-11-18Fixed #30988 -- Deprecated the InvalidQuery exception.Simon Charette
It was barely documented without pointers at its defining location and was abused to prevent misuse of the QuerySet field deferring feature.
2019-11-18Replaced QueryWrapper single usage with RawSQL.Simon Charette
2019-10-01Fixed #30651 -- Made __eq__() methods return NotImplemented for not ↵ElizabethU
implemented comparisons. Changed __eq__ to return NotImplemented instead of False if compared to an object of the same type, as is recommended by the Python data model reference. Now these models can be compared to ANY (or other objects with __eq__ overwritten) without returning False automatically.
2019-07-25Refs #30657 -- Made DeferredAttribute.__init__() to take a field instance ↵Jon Dufresne
instead of a field name.
2019-04-03Fixed typo in django/db/models/query_utils.py comment.David Beitey
2018-10-08Replaced kwargs.pop() with keyword-only arguments.Jon Dufresne
2018-09-28Refs #28909 -- Simplifed code using unpacking generalizations.Sergey Fedoseev
2018-02-12Refs #29125 -- Made Q.deconstruct() omit 'query_utils' in the path and ↵Tim Graham
_connector='AND' since it's a default value.
2018-02-12Fixed #29125 -- Made Q.deconstruct() deterministic with multiple keyword ↵Tim Graham
arguments.