django signals 信号

 

django signals 信号

 

配置方式

app下的 __init__.py

default_app_config="web.apps.WebConfig"    
#初始化app配置
#调用app下面的类

 

同一个app下的 apps.py

from django.apps import AppConfig    #导入

#app配置
class WebConfig(AppConfig):   #必须继承这个类 
    name = 'web'    #app
    

    def ready(self):
        import web.signals.trade_signal
        import web.signals.study_signal
        import web.signals.notice_signal

 

信号

from django.db.models.signals import post_save    #保存后

from django.db.models.signals import pre_save    #保存前

from django.dispatch import receiver    #使用信号的装饰器 检测Django 模型对象字段值的变化

from web import models    #数据库

 

orm操作前触发

@receiver(pre_save, sender=models.Order)
def get_order_status(sender, instance=None, **kwargs):
    """
    获取订单状态
    :param sender:
    :param instance:    #当前对象
    :param kwargs:
    :return:
    """
#对象保存前

orm操作后触发

@receiver(post_save, sender=models.Order)
def create_enrolled_record(sender, instance=None, created=False, **kwargs):
    """
    :param sender:
    :param instance:保存后的对象
    :param created: 是否是创建新的记录
    :param kwargs:
    :return:
    """
#保存对象后

 

signals.py

import warnings
from functools import partial

from django.db.models.utils import make_model_tuple
from django.dispatch import Signal
from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning


class_prepared = Signal(providing_args=["class"])


class ModelSignal(Signal):
    """
    Signal subclass that allows the sender to be lazily specified as a string
    of the `app_label.ModelName` form.
    """
    def _lazy_method(self, method, apps, receiver, sender, **kwargs):
        from django.db.models.options import Options

        # This partial takes a single optional argument named "sender".
        partial_method = partial(method, receiver, **kwargs)
        if isinstance(sender, six.string_types):
            apps = apps or Options.default_apps
            apps.lazy_model_operation(partial_method, make_model_tuple(sender))
        else:
            return partial_method(sender)

    def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None):
        self._lazy_method(
            super(ModelSignal, self).connect, apps, receiver, sender,
            weak=weak, dispatch_uid=dispatch_uid,
        )

    def disconnect(self, receiver=None, sender=None, weak=None, dispatch_uid=None, apps=None):
        if weak is not None:
            warnings.warn("Passing `weak` to disconnect has no effect.", RemovedInDjango20Warning, stacklevel=2)
        return self._lazy_method(
            super(ModelSignal, self).disconnect, apps, receiver, sender, dispatch_uid=dispatch_uid
        )


pre_init = ModelSignal(providing_args=["instance", "args", "kwargs"], use_caching=True)
post_init = ModelSignal(providing_args=["instance"], use_caching=True)

pre_save = ModelSignal(providing_args=["instance", "raw", "using", "update_fields"],
                       use_caching=True)
post_save = ModelSignal(providing_args=["instance", "raw", "created", "using", "update_fields"], use_caching=True)

pre_delete = ModelSignal(providing_args=["instance", "using"], use_caching=True)
post_delete = ModelSignal(providing_args=["instance", "using"], use_caching=True)

m2m_changed = ModelSignal(
    providing_args=["action", "instance", "reverse", "model", "pk_set", "using"],
    use_caching=True,
)

pre_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using", "apps", "plan"])
post_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using", "apps", "plan"])

  

 

posted @ 2017-10-23 12:38  陨落&新生  阅读(2264)  评论(0编辑  收藏  举报