Override QuerySet.delete() (one way of preventing cascading deletes)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.db.models.query import QuerySet

#save original delete method
orrigdelete = QuerySet.delete
def showdelete(self):
    #add on to delete method
    for test in self:
        if isinstance(test, YourObject):
            raise Exception('someone tried to delete your object')
            return  
        else:
            break   
    #call original delete
    return orrigdelete(self)

#set the queryset delete as our new method
QuerySet.delete = showdelete

More like this

  1. Middleware for using HttpOnly session cookie (including monkey patching for support for Python <2.6) by chrj 1 year, 9 months ago
  2. Monkey-patch Django's test client to return WSGIRequest objects by robmadole 1 year, 2 months ago
  3. UUIDField oriented django User by bwhittington 2 years, 9 months ago
  4. decorators for creating paramaterized decorators and easy monkeypatching by fish2000 2 years ago
  5. safe(r) monkeypatching scheme for django testing by showell 2 years, 2 months ago

Comments

(Forgotten your password?)