2
13. Adding hooks before/after saving and deleting
4
To execute arbitrary code around ``save()`` and ``delete()``, just subclass
7
from __future__ import unicode_literals
9
from django.db import models
10
from django.utils.encoding import python_2_unicode_compatible
13
@python_2_unicode_compatible
14
class Person(models.Model):
15
first_name = models.CharField(max_length=20)
16
last_name = models.CharField(max_length=20)
18
def __init__(self, *args, **kwargs):
19
super(Person, self).__init__(*args, **kwargs)
23
return "%s %s" % (self.first_name, self.last_name)
25
def save(self, *args, **kwargs):
26
self.data.append("Before save")
27
# Call the "real" save() method
28
super(Person, self).save(*args, **kwargs)
29
self.data.append("After save")
32
self.data.append("Before deletion")
33
# Call the "real" delete() method
34
super(Person, self).delete()
35
self.data.append("After deletion")