Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #280 -- Added _pre_delete() and _post_delete() hooks. Also upda…

…ted the docs and added a unit testdjango/core/meta/__init__.py

git-svn-id: http://code.djangoproject.com/svn/django/trunk@432 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b7e5121684afa36739c1aa9bbb96401645dbc417 1 parent 03bd1d7
Adrian Holovaty authored August 08, 2005
6  docs/model-api.txt
@@ -728,6 +728,12 @@ There are a few object methods that have special meaning:
728 728
     This method is called just after the object is saved to the database. This
729 729
     could be used to update other tables, update cached information, etc.
730 730
 
  731
+``_pre_delete``
  732
+    Like ``_pre_save``, but for deletion.
  733
+
  734
+``_post_delete``
  735
+    Like ``_post_save``, but for deletion.
  736
+
731 737
 Module-level methods
732 738
 --------------------
733 739
 
51  tests/testapp/models/save_delete_hooks.py
... ...
@@ -0,0 +1,51 @@
  1
+"""
  2
+13. Adding hooks before/after saving and deleting.
  3
+
  4
+Django provides hooks for executing arbitrary code around ``save()`` and
  5
+``delete()``. Just add any of the following methods to your model:
  6
+
  7
+    * ``_pre_save()`` is called before an object is saved.
  8
+    * ``_post_save()`` is called after an object is saved.
  9
+    * ``_pre_delete()`` is called before an object is deleted.
  10
+    * ``_post_delete()`` is called after an object is deleted.
  11
+"""
  12
+
  13
+from django.core import meta
  14
+
  15
+class Person(meta.Model):
  16
+    fields = (
  17
+        meta.CharField('first_name', maxlength=20),
  18
+        meta.CharField('last_name', maxlength=20),
  19
+    )
  20
+
  21
+    def __repr__(self):
  22
+        return "%s %s" % (self.first_name, self.last_name)
  23
+
  24
+    def _pre_save(self):
  25
+        print "Before save"
  26
+
  27
+    def _post_save(self):
  28
+        print "After save"
  29
+
  30
+    def _pre_delete(self):
  31
+        print "Before deletion"
  32
+
  33
+    def _post_delete(self):
  34
+        print "After deletion"
  35
+
  36
+API_TESTS = """
  37
+>>> p1 = persons.Person(first_name='John', last_name='Smith')
  38
+>>> p1.save()
  39
+Before save
  40
+After save
  41
+
  42
+>>> persons.get_list()
  43
+[John Smith]
  44
+
  45
+>>> p1.delete()
  46
+Before deletion
  47
+After deletion
  48
+
  49
+>>> persons.get_list()
  50
+[]
  51
+"""

0 notes on commit b7e5121

Please sign in to comment.
Something went wrong with that request. Please try again.