summaryrefslogtreecommitdiff
path: root/django/db/models/query.py
AgeCommit message (Collapse)Author
2026-04-19Refs #28586 -- Added DEFAULT_FETCH_MODE module constant.Jacob Walls
This is a more attractive target for alteration than all of QuerySet.__init__().
2026-04-03Fixed #37016 -- Avoided propagating invalid arguments from When() to Q().varunkasyap
2026-03-11Refs #28455 -- Avoided QuerySet cloning in simple prefetch_related() usages.Keryn Knight
manager.get_queryset() always returns freshly instantiated per-instance QuerySet which doesn't need subsequent cloning. Based on work originally done by Anssi Kääriäinen and Tim Graham.
2026-03-11Refs #28455 -- Implemented private API methods for preventing QuerySet cloning.Keryn Knight
Multiple calls are idempotent assuming they're balanced. Also, multiple calls to disable cloning followed by a single call to re-enable cloning will subsequently cause clones to occur - it is not a stack, just a toggle. @contextlib.contextmanager is intentionally not used for performance reasons: - decorator takes 1.1µs to execute, or 2µs if used correctly in a `with ...:` statement - custom class takes 300ns to execute, or 900ns if used correctly in a `with ...:` statement Based on work originally done by Anssi Kääriäinen and Tim Graham.
2026-02-13Fixed #36857 -- Added QuerySet.totally_ordered property.VIZZARD-X
Thanks Simon Charette for the idea.
2026-02-06Fixed #36644 -- Enabled empty order_by() to avoid pk ordering by first()/last().Nilesh Kumar Pahari
2026-02-06Refs #36644 -- Applied default ordering after union().Nilesh Kumar Pahari
2026-01-13Fixed #35442 -- Prevented N+1 queries in RelatedManager with only().Samriddha9619
Co-authored-by: Simon Charette <charette.s@gmail.com>
2025-11-06Fixed #36702 -- Made bulk_create() return pk values set by an expression.us77ipis
2025-11-05Refs CVE-2025-64459 -- Avoided propagating invalid arguments to Q on ↵Jacob Walls
dictionary expansion.
2025-10-25Fixed #36683 -- Added error message on QuerySet.update() following ↵Matthew Shirley
distinct(*fields).
2025-10-16Refs #28586 - Copied fetch mode in QuerySet.create().Adam Johnson
This change allows the pattern `MyModel.objects.fetch_mode(...).create(...)` to set the fetch mode for a new object.
2025-10-16Refs #28586 -- Copied fetch modes to related objects.Adam Johnson
This change ensures that behavior and performance remain consistent when traversing relationships.
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-10-14Fixed #36648, Refs #33772 -- Accounted for composite pks in first()/last() ↵Jacob Walls
when aggregating.
2025-09-30Fixed #36614 -- Deprecated QuerySet.values_list(flat=True) without a field.Adam Johnson
Thanks to Jacob Walls and Simon Charette for their input. co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
2025-09-25Refs #36605 -- Optimized QuerySet.in_bulk() for the empty id_list case.Adam Johnson
Now that the setup is a bit more expensive, it makes sense to return earlier for the empty case.
2025-09-25Fixed #36605 -- Added support for QuerySet.in_bulk() after .values() or ↵John Parton
.values_list(). co-authored-by: Adam Johnson <me@adamj.eu> co-authored-by: Simon Charette <charette.s@gmail.com>
2025-09-22Fixed #35453 -- Made ManyToManyField.concrete False.Ryan P Kilby
ManyToManyField was already excluded from fields, concrete_fields, and local_concrete_fields in Options.
2025-09-16Fixed #36606 -- Optimized QuerySet.values_list(flat=True) without fields.Adam Johnson
2025-09-16Fixed #36426 -- Added support for further iterables in ↵blingblin-g
prefetch_related_objects(). Thanks Sarah Boyce for the review.
2025-09-14Refs #27222 -- Refreshed GeneratedFields values on save() initiated update.Simon Charette
This required implementing UPDATE RETURNING machinery that heavily borrows from the INSERT one.
2025-08-29Refs #36152 -- Suppressed duplicate warning when using "%" in alias via ↵Jacob Walls
values().
2025-08-21Refs #36430, #36416, #34378 -- Simplified batch size calculation in ↵Jacob Walls
QuerySet.in_bulk().
2025-08-15Refs #36490 -- Simplified QuerySet._batched_insert returning fields handling.Simon Charette
Whether or not returning_fields should be specified to _insert is not a function of each batches so the conditional can be moved outside of the loop.
2025-08-15Fixed #36490 -- Avoided unnecessary transaction in bulk_create.Simon Charette
When dealing with an heterogeneous set of object with regards to primary key assignment that fits in a single batch there's no need to wrap the single INSERT statement in a transaction.
2025-07-23Refs #36500 -- Corrected rewrapped long lines fixed via a script.Mike Edmunds
Manually reformatted some comments and docstrings where autofix_w505.py changed the meaning of the formatting.
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-07-23Removed double spaces after periods and within phrases.Sarah Boyce
2025-06-03Fixed #36416 -- Made QuerySet.in_bulk() account for composite pks in id_list.Jacob Walls
2025-06-03Fixed #36060 -- Prevented IntegrityError in bulk_create() with ↵myoungjinGo-BE
order_with_respect_to.
2025-05-19Fixed #36388 -- Made QuerySet.union() return self when called with no arguments.Colleen Dunlap
Regression in 9cb8baa0c4fa2c10789c5c8b65f4465932d4d172. Thank you to Antoine Humeau for the report and Simon Charette for the review.
2025-05-12Fixed #36373 -- Fixed select_related() crash on foreign object for a ↵Simon Charette
composite pk. Thanks Jacob Walls for the report and Sarah for the in-depth review.
2025-04-30Fixed #36360 -- Fixed QuerySet.update() crash when referring annotations ↵Simon Charette
through values(). The issue was only manifesting itself when also filtering againt a related model as that forces the usage of a subquery because SQLUpdateCompiler doesn't support the UPDATE FROM syntax yet. Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a. Refs #28900. Thanks Gav O'Connor for the detailed report.
2025-04-26Refs #36326 -- Fixed QuerySet.raw() crash on models with CompositePrimaryKey ↵Mariusz Felisiak
on Oracle. Virtual CompositePrimaryKey fields should be ignored. Regression in 1831f7733d3ef03d1ca7fac3e8d9f4c5e3e3375e.
2025-04-17Fixed #36326 -- Added CompositePrimaryKey support in QuerySet.raw().SaJH
Signed-off-by: SaJH <wogur981208@gmail.com>
2025-04-11Fixed #36288 -- Addressed improper handling of duplicates in values_list().Simon Charette
Now that selected aliases are stored in sql.Query.selected: dict[str, Any] the values_list() method must ensures that duplicate field name references are assigned unique aliases. Refs #28900. Regression in 65ad4ade74dc9208b9d686a451cd6045df0c9c3a. Thanks Claude for the report.
2025-03-31Refs #28909 -- Simplified code using unpacking generalizations.Aarni Koskela
2025-03-26Refs #36260 -- Moved _is_pk_set checks into _prepare_for_bulk_create().Simon Charette
To avoid looping over objs twice.
2025-03-26Fixed #36260 -- Made bulk_create() work with DB-generated primary keys.Dmitry Shachnev
Co-authored-by: Simon Charette <charette.s@gmail.com>
2025-02-11Fixed #36149 -- Allowed subquery values against tuple exact and in lookups.Simon Charette
Non-tuple exact and in lookups have specialized logic for subqueries that can be adapted to properly assign select mask if unspecified and ensure the number of involved members are matching on both side of the operator.
2025-02-01Fixed #36088 -- Avoided unnecessary DEFAULT usage on bulk_create().Simon Charette
When all values of a field with a db_default are DatabaseDefault, which is the case most of the time, there is no point in specifying explicit DEFAULT for all INSERT VALUES as that's what the database will do anyway if not specified. In the case of PostgreSQL doing so can even be harmful as it prevents the usage of the UNNEST strategy and in the case of Oracle, which doesn't support the usage of the DEFAULT keyword, it unnecessarily requires providing literal db defaults. Thanks Lily Foote for the review.
2025-01-30Fixed #36155 -- Improved error handling when annotate arguments require an ↵Vinko Mlačić
alias. Regression in ed0cbc8d8b314e3b4a0305d0be3cf366d8ee4a74.
2025-01-29Fixed #36118 -- Accounted for multiple primary keys in bulk_update ↵Sarah Boyce
max_batch_size. Co-authored-by: Simon Charette <charette.s@gmail.com>
2025-01-15Refs #33651 -- Removed Prefetch.get_current_queryset() and ↵Sarah Boyce
get_prefetch_queryset() per deprecation timeline.
2025-01-14Refs #36075 -- Adjusted pk_fields usage in bulk_update eligibility checks.Simon Charette
Regression in bf7b17d16d3978b2e1cee4a0f7ce8840bd1a8dc4. Thanks Sage Abdullah for the report.
2025-01-13Refs #36075 -- Used field in pk_fields over field.primary_key.Sarah Boyce
2025-01-10Fixed #36068 -- Raised ValueError when providing a composite PK field to ↵Jacob Walls
bulk_create() update_fields.
2025-01-03Fixed #35918 -- Added support for execute_sql to directly return row counts.Raphael Gaschignard
2025-01-02Fixed typo in django/db/models/query.py docstring.Jacob Walls