监视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 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7760e5609bea5fe1340928f3160e368c6a444c94614a3058a6707768c0171791 |
|
MD5 | fe26550cbd8237aaccfff2d903855002 |
|
BLAKE2b-256 | c7954e8d30e622af00055fd242833031e177476610db27d4385f6375752891bb |