~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/save_delete_hooks/models.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
13. Adding hooks before/after saving and deleting
 
3
 
 
4
To execute arbitrary code around ``save()`` and ``delete()``, just subclass
 
5
the methods.
 
6
"""
 
7
from __future__ import unicode_literals
 
8
 
 
9
from django.db import models
 
10
from django.utils.encoding import python_2_unicode_compatible
 
11
 
 
12
 
 
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)
 
17
 
 
18
    def __init__(self, *args, **kwargs):
 
19
        super(Person, self).__init__(*args, **kwargs)
 
20
        self.data = []
 
21
 
 
22
    def __str__(self):
 
23
        return "%s %s" % (self.first_name, self.last_name)
 
24
 
 
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")
 
30
 
 
31
    def delete(self):
 
32
        self.data.append("Before deletion")
 
33
        # Call the "real" delete() method
 
34
        super(Person, self).delete()
 
35
        self.data.append("After deletion")