diff --git a/django/db/models/manager.py b/django/db/models/manager.py index 306a92644a..52a4d67bd1 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -100,7 +100,7 @@ class Manager(object): # Check if extra tables are allowed. If not, throw an error if (tables or joins) and not allow_joins: - raise TypeError("Joins are not allowed in this type of query") + raise TypeError, "Joins are not allowed in this type of query" # Compose the join dictionary into SQL describing the joins. if joins: @@ -150,6 +150,11 @@ class Manager(object): return select, " ".join(sql), params def delete(self, *args, **kwargs): + nArguments = len(args) + len(kwargs) + + # remove the DELETE_ALL argument, if it exists + delete_all = kwargs.pop('DELETE_ALL', False) + # disable non-supported fields kwargs['select_related'] = False kwargs['select'] = {} @@ -157,6 +162,10 @@ class Manager(object): kwargs['offset'] = None kwargs['limit'] = None + # Check that there at least one query argument + if nArguments == 0 and not delete_all: + raise TypeError, "SAFTEY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data" + opts = self.klass._meta # Perform the SQL delete diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py index 94c15692d0..1262217e08 100644 --- a/tests/modeltests/basic/models.py +++ b/tests/modeltests/basic/models.py @@ -210,6 +210,15 @@ AttributeError: Manager isn't accessible via Article instances >>> Article.objects.get_count() 4L +>>> Article.objects.delete() +Traceback (most recent call last): + ... +TypeError: SAFTEY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data + +>>> Article.objects.delete(DELETE_ALL=True) +>>> Article.objects.get_count() +0L + """ from django.conf import settings