跳转到主要内容

监视Django的任何对象/字段/关系/通用关系并接收信号

项目描述

监视Django模型中任何类型字段的修改并调用已注册的回调函数

此观察者可以监视

  • 任何值类型的字段(CharField,IntegerField……)

  • 任何模型类型的字段(ForeignKey,OneToOneField,GenericForeignKey)

  • 任何相关管理器类型的字段(通过ForeignKey的related_name自动创建的字段)

  • 任何多相关管理器类型的字段(ManyToManyField)

  • 任何通用相关对象管理器类型的字段(GenericRelation)

  • 任何模型实例

安装

此库位于PyPI,因此您可以使用以下命令安装它

pip install django-observer

或从github

pip install git+https://github.com/lambdalisue/django-observer.git

用法

>>> from django.db import models
>>> from observer import watch
>>>
>>> class Entry(models.Model):
...     status = models.CharFiled('status', max_length=10)
...     body = models.CharField('title', max_length=100)
...
...     def save(self, *args, **kwargs):
...         super(Entry, self).save(*args, **kwargs)
...
...         # Watch callback, this is automatically called if `status` is **changed**
...         def watch_callback(sender, obj, attr):
...             # sender is a watcher instance
...             # obj is a instance of target
...             # attr is a name of target field
...             if obj.status == 'draft':
...                 obj.title = "draft - %s" % obj.body
...             else:
...                 obj.title = "publish - %s" % obj.body
...         # Start watching
...         self._watcher = watch(self, 'status', watch_callback)

有关更多详细信息,请参阅observer_test/src/miniblog/blog/tests/test_observer.py

设置

OBSERVER_DEFAULT_WATCHER

观察者类。默认为‘observer.watcher.complex.ComplexWatcher’

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

django-observer-0.4.3.tar.gz (7.4 kB 查看哈希值)

上传时间

由以下支持