利用Django的Model的Signal Dispatcher,通过django.db.models.signals.pre_save()方法,在事件发生前,发射触发信号,这一切都被调度中的receiver方法深藏功与名的保存了。
信号的处理一般都写在Model中,举个例子:
import logging
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver
class Order(models.Model):
logger = logging.getLogger(__name__)
@receiver(pre_save, sender=Order)
def pre_save_handler(sender, **kwargs):
logger.debug("{},{}".format(sender, **kwargs))
网友评论