Django中提供了“信号调度”,用于在框架执行操作时解耦。通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者
1、Django内置信号
Model signals pre_init # django的modal执行其构造方法前,自动触发 post_init # django的modal执行其构造方法后,自动触发 pre_save # django的modal对象保存前,自动触发 post_save # django的modal对象保存后,自动触发 pre_delete # django的modal对象删除前,自动触发 post_delete # django的modal对象删除后,自动触发 m2m_changed # django的modal中使用m2m字段操作第三张表(add,remove,clear)前后,自动触发 class_prepared # 程序启动时,检测已注册的app中modal类,对于每一个类,自动触发 Management signals pre_migrate # 执行migrate命令前,自动触发 post_migrate # 执行migrate命令后,自动触发 Request/response signals request_started # 请求到来前,自动触发 request_finished # 请求结束后,自动触发 got_request_exception # 请求异常后,自动触发 Test signals setting_changed # 使用test测试修改配置文件时,自动触发 template_rendered # 使用test测试渲染模板时,自动触发 Database Wrappers connection_created # 创建数据库连接时,自动触发
这个信号 django内部已经全部定死 已经埋下触发信号操作
于Django内置的信号,仅需注册指定信号,当程序执行相应操作时,自动触发注册函数:
要注册 先导入
from django.core.signals import request_finished from django.core.signals import request_started from django.core.signals import got_request_exception from django.db.models.signals import class_prepared from django.db.models.signals import pre_init, post_init from django.db.models.signals import pre_save, post_save from django.db.models.signals import pre_delete, post_delete from django.db.models.signals import m2m_changed from django.db.models.signals import pre_migrate, post_migrate from django.test.signals import setting_changed from django.test.signals import template_rendered from django.db.backends.signals import connection_created def callback(sender, **kwargs): print("xxoo_callback") print(sender,kwargs) xxoo.connect(callback) # xxoo指上述导入的内容
xxoo是什么 比如
.connect() 里面 传的 是一个自定义函数 代表在信号里面 注册一个函数
from django.core.signals import request_finished from django.core.signals import request_started from django.core.signals import got_request_exception from django.db.models.signals import class_prepared from django.db.models.signals import pre_init, post_init from django.db.models.signals import pre_save, post_save from django.db.models.signals import pre_delete, post_delete from django.db.models.signals import m2m_changed from django.db.models.signals import pre_migrate, post_migrate from django.test.signals import setting_changed from django.test.signals import template_rendered from django.db.backends.signals import connection_created def f1(sender, **kwargs): print("end") pre_init.connect(f1)
也可以注册多个函数 按照顺序执行
from django.core.signals import request_finished from django.core.signals import request_started from django.core.signals import got_request_exception from django.db.models.signals import class_prepared from django.db.models.signals import pre_init, post_init from django.db.models.signals import pre_save, post_save from django.db.models.signals import pre_delete, post_delete from django.db.models.signals import m2m_changed from django.db.models.signals import pre_migrate, post_migrate from django.test.signals import setting_changed from django.test.signals import template_rendered from django.db.backends.signals import connection_created def f1(sender, **kwargs): print("end") def f2(sender, **kwargs): print("end22") pre_init.connect(f1) pre_init.connect(f2)
想在django 应用 注册的函数的 信号注册到django
在django项目 创建一个sg.py 文件
from django.core.signals import request_finished from django.core.signals import request_started from django.core.signals import got_request_exception from django.db.models.signals import class_prepared from django.db.models.signals import pre_init, post_init from django.db.models.signals import pre_save, post_save from django.db.models.signals import pre_delete, post_delete from django.db.models.signals import m2m_changed from django.db.models.signals import pre_migrate, post_migrate from django.test.signals import setting_changed from django.test.signals import template_rendered from django.db.backends.signals import connection_created def f1(sender, **kwargs): print("xxoo") pre_init.connect(f1)
django执行 models构造方法前 自动触发信号
在django项目中的 __init__.py 文件 导入 sg.py 模块 那就django程序一运行,自动把sg.py 代码加载到内存,然后相应信号函数就注册了
urls.py signal函数
from django.conf.urls import url from django.contrib import admin from django.conf.urls import include from app01 import views urlpatterns = [ # url(r‘^admin/‘, admin.site.urls), url(r‘^login/‘, views.login), url(r‘^index/‘, views.index), url(r‘^logout/‘, views.logout), url(r‘^test/‘, views.test), url(r‘^cache/‘, views.cache), url(r‘^signal/‘, views.signal), ]
views.py signal函数 用来测试注册的信号
def signal(request): from app01 import models obj = models.UserInfo(username=‘mike‘) print("end") obj.save() obj = models.UserInfo(username=‘mike‘) print("end1") obj.save() obj = models.UserInfo(username=‘mike‘) print("end2") obj.save() return HttpResponse("ok")
一启动django 注册的函数马上加载到内存了
xxoo xxoo System check identified no issues (0 silenced). January 07, 2019 - 17:17:06 Django version 1.11, using settings ‘day21django.settings‘ Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK
执行django signal url
后台拿到信息
每一次 执行 models构造方法前 自动触发信号 函数 然后打印信息
mike jack tom 小张 小刚 老张 xxoo end xxoo end1 xxoo end2 xiaogang xiaoming peter [07/Jan/2019 17:25:01] "GET /signal/ HTTP/1.1" 200 2
原文地址:https://www.cnblogs.com/mingerlcm/p/10226906.html
时间: 2024-10-20 20:43:41