Permalink
Browse files

adding a cascade keyword argument to Model.Delete and QuerySet.delete…

…. See #1.

cascade currently defaults to False to conserve existing
behaviour. Ideally this will change in the future, so we warn the user
about the future change.
  • Loading branch information...
1 parent 806939d commit 772e2387b0b32f817403d6eaddf38c7ca204b180 Wilfred Hughes committed Dec 17, 2011
Showing with 19 additions and 5 deletions.
  1. +9 −2 django/db/models/base.py
  2. +10 −3 django/db/models/query.py
View
@@ -1,5 +1,6 @@
import types
import sys
+import warnings
from itertools import izip
import django.db.models.manager # Imported to register signal handler.
@@ -598,12 +599,18 @@ def save_base(self, raw=False, cls=None, origin=None, force_insert=False,
save_base.alters_data = True
- def delete(self, using=None):
+ def delete(self, using=None, cascade=None):
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
+ if cascade is None:
+ warnings.warn("You have not specified cascade behavior for deletion. "
+ "This delete won't cascade, but this will change in a future Django-nonrel release.",
+ RuntimeWarning)
+ cascade = False
+
collector = Collector(using=using)
- collector.collect([self])
+ collector.collect([self], collect_related=cascade)
collector.delete()
self._entity_exists = False
View
@@ -2,6 +2,7 @@
The main QuerySet implementation. This provides the public API for the ORM.
"""
+import warnings
from itertools import izip
from django.db import connections, router, transaction, IntegrityError
@@ -424,7 +425,7 @@ def in_bulk(self, id_list):
qs.query.clear_ordering(force_empty=True)
return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
- def delete(self):
+ def delete(self, cascade=None):
"""
Deletes the records in the current QuerySet.
"""
@@ -442,8 +443,14 @@ def delete(self):
del_query.query.select_related = False
del_query.query.clear_ordering()
+ if cascade is None:
+ warnings.warn("You have not specified cascade behavior for deletion. "
+ "This delete won't cascade, but this will change in a future Django-nonrel release.",
+ RuntimeWarning)
+ cascade = False
+
collector = Collector(using=del_query.db)
- collector.collect(del_query)
+ collector.collect(del_query, collect_related=cascade)
collector.delete()
# Clear the result cache, in case this QuerySet gets reused.
@@ -1028,7 +1035,7 @@ def __or__(self, other):
def count(self):
return 0
- def delete(self):
+ def delete(self, *args, **kwargs):
pass
def _clone(self, klass=None, setup=False, **kwargs):

0 comments on commit 772e238

Please sign in to comment.