PageRenderTime 82ms CodeModel.GetById 41ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/tests/modeltests/save_delete_hooks/models.py

https://code.google.com/p/mango-py/
Python | 32 lines | 17 code | 7 blank | 8 comment | 0 complexity | 6de0a49802d5b67c7dae102183a8660a MD5 | raw file
 1"""
 213. Adding hooks before/after saving and deleting
 3
 4To execute arbitrary code around ``save()`` and ``delete()``, just subclass
 5the methods.
 6"""
 7
 8from django.db import models
 9
10
11class Person(models.Model):
12    first_name = models.CharField(max_length=20)
13    last_name = models.CharField(max_length=20)
14
15    def __init__(self, *args, **kwargs):
16        super(Person, self).__init__(*args, **kwargs)
17        self.data = []
18
19    def __unicode__(self):
20        return u"%s %s" % (self.first_name, self.last_name)
21
22    def save(self, *args, **kwargs):
23        self.data.append("Before save")
24         # Call the "real" save() method
25        super(Person, self).save(*args, **kwargs)
26        self.data.append("After save")
27
28    def delete(self):
29        self.data.append("Before deletion")
30        # Call the "real" delete() method
31        super(Person, self).delete()
32        self.data.append("After deletion")