summaryrefslogtreecommitdiff
path: root/django/db/models/deletion.py
AgeCommit message (Collapse)Author
2025-10-18Fixed #21961 -- Added support for database-level delete options for ForeignKey.Mariusz Felisiak
Thanks Simon Charette for pair programming. Co-authored-by: Nick Stefan <NickStefan12@gmail.com> Co-authored-by: Akash Kumar Sen <71623442+Akash-Kumar-Sen@users.noreply.github.com> Co-authored-by: Simon Charette <charette.s@gmail.com>
2025-09-23Fixed #36264 -- Excluded proxy neighbors of parents from deletion collection ↵saJaeHyukc
when keep_parents=True. Signed-off-by: saJaeHyukc <wogur981208@gmail.com>
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-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>
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-02-08Fixed #35073 -- Avoided unnecessary calling of callables used by ↵bcail
SET/SET_DEFAULT in Collector.collect().
2022-08-27Fixed #33928 -- Avoided unnecessary queries when cascade updating.Simon Charette
Models that use SET, SET_NULL, and SET_DEFAULT as on_delete handler don't have to fetch objects for the sole purpose of passing them back to a follow up UPDATE query filtered by the retrieved objects primary key. This was achieved by flagging SET handlers as _lazy_ and having the collector logic defer object collections until the last minute. This should ensure that the rare cases where custom on_delete handlers are defined remain uncalled when when dealing with an empty collection of instances. This reduces the number queries required to apply SET handlers from 2 to 1 where the remaining UPDATE use the same predicate as the non removed SELECT query. In a lot of ways this is similar to the fast-delete optimization that was added in #18676 but for updates this time. The conditions only happen to be simpler in this case because SET handlers are always terminal. They never cascade to more deletes that can be combined. Thanks Renan GEHAN for the report.
2022-08-27Refs #33928 -- Removed unnecessary attribute assignment on on-delete updates.Simon Charette
Model instances retrieved for bulk field update purposes are not exposed to the outside world and thus are not required to be kept update to date.
2022-07-27Refs #32948, Refs #32946 -- Used Q.create() internally for dynamic Q() objects.Nick Pope
Node.create() which has a compatible signature with Node.__init__() takes in a single `children` argument rather than relying in unpacking *args in Q.__init__() which calls Node.__init__(). In addition, we were often needing to unpack iterables into *args and can instead pass a list direct to Node.create().
2022-02-07Refs #33476 -- Reformatted code with Black.django-bot
2022-01-11Fixed #13251 -- Made pre/post_delete signals dispatch the origin.mgaligniana
2021-07-28Refs #32946 -- Changed internal usage of dynamic Q() objects construction to ↵Keryn Knight
use non-kwargs initialization. This prefers non-kwargs construction of dynamically generated Q() objects to create a single Q() object instead of many and then combining them, where possible.
2020-10-19Fixed #32107 -- Fixed ProtectedError.protected_objects and ↵Hasan Ramezani
RestrictedError.restricted_objects. Regression in 4ca5c565f4dc9e97845036e86416abc5cfde766c and ab3cbd8b9a315911248227208630a020cedca08f. Thanks Vitaliy Yelnik for the report.
2020-05-29Refs #21171 -- Made Collector.delete() rollback in the correct database.Simon Charette
Regression in c7dd8490b882b2cefdc7faf431dc64c532b79c9.
2020-04-20Fixed #31474 -- Made QuerySet.delete() not return the number of deleted ↵Hasan Ramezani
objects if it's zero.
2020-01-31Fixed #31219 -- Fixed object deletion crash for nested protected related ↵Matthias Kestenholz
objects.
2020-01-29Refs #27852 -- Fixed object deletion to show all restricted related objects ↵Hasan Ramezani
rather than just the first one.
2020-01-29Refs #27852 -- Renamed a loop variable in Collector.collect() to avoid ↵Mariusz Felisiak
redefinition.
2020-01-27Refs #27852 -- Fixed object deletion to show all protected related objects ↵Hasan Ramezani
rather than just the first one. Thanks Anton Samarchyan for the initial patch.
2019-11-19Fixed #27272 -- Added an on_delete RESTRICT handler to allow cascading ↵Daniel Izquierdo
deletions while protecting direct ones.
2019-11-19Refs #27272 -- Added Collector.add_dependency().Daniel Izquierdo
2019-10-09Fixed #30856 -- Combined fast-delete queries by model during cascade deletion.Simon Charette
Reduced the number of queries required when performing cascade deletion for a model referenced multiple time by another one by performing an union of reference lookups.
2019-10-09Made Collector.collect() return immediately for disabled related collection.Simon Charette
2019-10-09Used defaultdict in deletion.Collector.Simon Charette
2019-05-06Fixed #30339 -- Made Model.delete(keep_parents=True) preserves nested parent ↵Stephen Brown
reverse relationships. Thanks Simon Charette for the review.
2019-04-17Fixed #30191 -- Selected only referenced fields during cascade deletion.Simon Charette
The non-referenced fields can only be deferred if no deletion signals receivers are connected for their respective model as connected as these receivers might expect all fields of the deleted model to be present. Thanks Ed Morley for the report.
2019-04-17Refs #18676 -- Enabled fast-delete for m2m_changed senders.Simon Charette
There's no reason to disable fast-delete when an intermediary many-to-many model has connected m2m_changed receivers because the signal is only sent when related manager's clear() and remove() methods are directly called. This must have been overlooked in 1cd6e04cd4f768bcd4385b75de433d497d938f82 given no regression tests fail when m2m_changed is not taken into consideration to determine if fast-delete can be enabled.
2019-04-08Fixed #30330 -- Fixed setting of primary key to None during fast-delete.Florian Apolloner
Regression in bc7dd8490b882b2cefdc7faf431dc64c532b79c9.
2019-02-06Fixed #30159 -- Removed unneeded use of OrderedDict.Nick Pope
Dicts preserve order since Python 3.6.
2018-10-17Fixed #21171 -- Avoided starting a transaction when a single (or atomic ↵Florian Apolloner
queries) are executed. Checked the following locations: * Model.save(): If there are parents involved, take the safe way and use transactions since this should be an all or nothing operation. If the model has no parents: * Signals are executed before and after the previous existing transaction -- they were never been part of the transaction. * if `force_insert` is set then only one query is executed -> atomic by definition and no transaction needed. * same applies to `force_update`. * If a primary key is set and no `force_*` is set Django will try an UPDATE and if that returns zero rows it tries an INSERT. The first case is completly save (single query). In the second case a transaction should not produce different results since the update query is basically a no-op then (might miss something though). * QuerySet.update(): no signals issued, single query -> no transaction needed. * Model/Collector.delete(): This one is fun due to the fact that is does many things at once. Most importantly though: It does send signals as part of the transaction, so for maximum backwards compatibility we need to be conservative. To ensure maximum compatibility the transaction here is removed only if the following holds true: * A single instance is being deleted. * There are no signal handlers attached to that instance. * There are no deletions/updates to cascade. * There are no parents which also need deletion.
2018-01-13Fixed #29016 -- Fixed incorrect foreign key nullification on related ↵Étienne Loks
instance deletion.
2017-12-26Fixed #28930 -- Simplified code with any() and all().Дилян Палаузов
2017-12-06Fixed #28893 -- Removed unnecessary dict.items() calls.Tim Graham
2017-02-28Refs #27656 -- Updated django.db docstring verbs according to PEP 257.Anton Samarchyan
2017-01-25Refs #23919 -- Replaced super(ClassName, self) with super().chillaranand
2017-01-19Refs #23919 -- Stopped inheriting from object to define new style classes.Simon Charette
2017-01-18Refs #23919 -- Removed most of remaining six usageClaude Paroz
Thanks Tim Graham for the review.
2016-11-15Fixed #27407 -- Made Model.delete(keep_parents=True) preserve parent reverse ↵Simon Charette
relationships. Thanks Tim for the review.
2016-09-08Refs #15250 -- Removed an obsolete comment regarding MTI cascade deletion.Simon Charette
2016-04-13Refs #16508 -- Renamed the current "virtual" fields to "private".Michal Petrucha
The only reason why GenericForeignKey and GenericRelation are stored separately inside _meta is that they need to be cloned for every model subclass, but that's not true for any other virtual field. Actually, it's only true for GenericRelation.
2016-04-08Fixed E128 flake8 warnings in django/.Tim Graham
2016-04-04Fixed W503 flake8 warnings.Tim Graham
2015-11-09Fixed #24576 -- Made deletion of related objects deterministic.Laura Feier
2015-10-12Refs #18012 -- Made proxy and concrete model reverse fields consistent.Simon Charette
Prior to this change proxy models reverse fields didn't include the reverse fields pointing to their concrete model.
2015-10-12Fixed #23076, #25505 -- Fixed deletion of intermediate proxy models.Simon Charette
Thanks to James Murty for his work on an alternate patch.
2015-10-12Refs #18012 -- Removed special casing for proxy models deletion.Simon Charette
This isn't required anymore now that reverse foreign keys from proxy models are propagated to their concrete model.
2015-09-03Fixed #25058 -- Added GenericRelations with related_query_name to the ↵sarthakmeh
admin's delete confirmation page.
2015-08-31Fixed #25331 -- Removed trailing blank lines in docstrings.Maxime Lorant
2015-05-22Fixed #16891 -- Made Model/QuerySet.delete() return the number of deleted ↵Alexander Sosnovskiy
objects.