magic-removal: Refs 1219 -- Added DELETE_ALL=True safety mechanism to prevent accidental deletion of entire table contents.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2047 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2006-01-18 13:16:58 +00:00
parent c2d468095d
commit 186c09d96f
2 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -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