Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Watch modification of any type of field in Django's model and call registered callback
Python Shell

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
docs
src/observer
tests
.coveragerc
.gitignore
.travis.yml
MANIFEST.in
README.rst
manage.py
requirements-docs.txt
requirements-test-py26.txt
requirements-test.txt
requirements.txt
runtests.py
setup.cfg
setup.py
tox.ini

README.rst

django-observer

Build status Coverage Downloads Latest version Wheel Status Egg Status License
Author
Alisue <lambdalisue@hashnote.net>
Supported python versions
Python 2.6, 2.7, 3.2, 3.3
Supported django versions
Django 1.2 - 1.6

Observe django model attribute modifications and call the specified callback. django-observer can recognize the modifications of

  • Any value type of fields (CharField, IntegerField, etc.)
  • Any relational fields (ForeignKey, OneToOneField, ManyToManyField)
  • Any reverse relational fields (fields given by related_name)
  • Any generic relational fields (GenericForeignKey, GenericRelation)

Documentation

http://django-observer.readthedocs.org/en/latest/

Installation

Use pip like:

$ pip install django-observer

Usage

Configuration

  1. Add observer to the INSTALLED_APPS in your settings module

    INSTALLED_APPS = (
        # ...
        'observer',
    )

Example

from django.db import models
from django.contrib.auth.models import User

def status_changed(sender, obj, attr):
    if obj.status == 'draft':
        obj.title = "Draft %s" % obj.title
    else:
        obj.title = obj.title.replace("Draft ")
    obj.save()

# watch status attribute via decorator
from observer.decorators import watch
@watch('status', status_changed, call_on_created=True)
class Entry(models.Model):
    title = models.CharField(max_length=30)
    status = models.CharFiled(max_length=10)

    body = models.TextField('title', max_length=100)
    author = models.ForeignKey(User, null=True, blank=True)


def author_changed(sender, obj, attr):
    if obj.author is None:
        obj.status = "draft"
        obj.save()

# watch author attribute via function
from observer.shortcuts import watch
watch(Entry, 'author', author_changed)
Something went wrong with that request. Please try again.