1、信号,其实就是钩子,可以在上面几种情况下定义执行某个函数,我们一般在project的__init__文件中定义,下面就是一个例子
2、用到模块需要导入,分别在下面的几个路径中
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
from django.db.models.signals import post_init
from django.db.models.signals import pre_save
from django.db.models.signals import post_save
from django.db.models.signals import pre_delete
from django.db.models.signals import post_delete
from django.db.models.signals import m2m_changed
from django.db.models.signals import pre_migrate
from django.db.models.signals import post_migrate
from django.test.signals import setting_changed
from django.test.signals import template_rendered
from django.db.backends.signals import connection_created
3、下面就是一个使用的例子,把函数注册到信号中
# 1、定义执行的函数
def callback1(sender,**kwargs):
print("before pre_migrate")
print(sender,kwargs)
# 定义执行的函数
def callback2(sender,**kwargs):
print("after pre_migrate")
print(sender,kwargs)
# 在执行migrate之前会执行的函数
pre_migrate.connect(callback1)
# 在执行migrate之后执行的函数
post_migrate.connect(callback2)
4、下面我们执行migrate命令就会有下面提示
E:\python\django_last>python manage.py migrate
Operations to perform:
Apply all migrations: admin, app1, auth, contenttypes, sessions
before pre_migrate
<AdminConfig: admin> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C755D0>, 'app_config': <AdminConfig: admin>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations
.state.StateApps object at 0x02CF1130>, 'plan': []}
before pre_migrate
<AuthConfig: auth> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C755D0>, 'app_config': <AuthConfig: auth>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations.sta
te.StateApps object at 0x02CF1130>, 'plan': []}
before pre_migrate
<ContentTypesConfig: contenttypes> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C755D0>, 'app_config': <ContentTypesConfig: contenttypes>, 'verbosity': 1, 'interactive': True, 'using': 'default', '
apps': <django.db.migrations.state.StateApps object at 0x02CF1130>, 'plan': []}
before pre_migrate
<SessionsConfig: sessions> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C755D0>, 'app_config': <SessionsConfig: sessions>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.d
b.migrations.state.StateApps object at 0x02CF1130>, 'plan': []}
before pre_migrate
<App1Config: app1> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C755D0>, 'app_config': <App1Config: app1>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations.sta
te.StateApps object at 0x02CF1130>, 'plan': []}
Running migrations:
No migrations to apply.
after pre_migrate
<AdminConfig: admin> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C75610>, 'app_config': <AdminConfig: admin>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations
.state.StateApps object at 0x03183FD0>, 'plan': []}
after pre_migrate
<AuthConfig: auth> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C75610>, 'app_config': <AuthConfig: auth>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations.sta
te.StateApps object at 0x03183FD0>, 'plan': []}
after pre_migrate
<ContentTypesConfig: contenttypes> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C75610>, 'app_config': <ContentTypesConfig: contenttypes>, 'verbosity': 1, 'interactive': True, 'using': 'default', '
apps': <django.db.migrations.state.StateApps object at 0x03183FD0>, 'plan': []}
after pre_migrate
<SessionsConfig: sessions> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C75610>, 'app_config': <SessionsConfig: sessions>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.d
b.migrations.state.StateApps object at 0x03183FD0>, 'plan': []}
after pre_migrate
<App1Config: app1> {'signal': <django.dispatch.dispatcher.Signal object at 0x01C75610>, 'app_config': <App1Config: app1>, 'verbosity': 1, 'interactive': True, 'using': 'default', 'apps': <django.db.migrations.sta
te.StateApps object at 0x03183FD0>, 'plan': []}
E:\python\django_last>
上面是python给我们定义的信号,我们还可以自定义信号,下面说一下如何自定义信号,也是写在projec的__init__文件中
1、注册我们的信号,和信号的里的函数
# 1、导入模块
import django.dispatch
# 2、注册一个信号
test = django.dispatch.Signal(providing_args=["aa","bb"])
# 这里的aa和bb相当于形参
# 3、定义一个触发信号要执行的函数
def callback3(sender,**kwargs):
print("自定义信号")
print(sender,kwargs)
# 4、给信号注册一个函数
test.connect(callback3)
2、在函数使用自定义的信号
# 使用我们自定义的信号
def cus_signal(request):
# 导入自定义的信号,这里需要注意,导入__init__下的信号,直接用下面的方式就可以了
from django_last import test
# 触发信号,这里要记得要传参数,sender的意思是 谁发送的信号
test.send(sender="自定义信号",aa="django",bb="python")
return HttpResponse("自定义信号")
3、效果如下
无论是django定义的信号,还是我们自定义的信号,我们就可以为一个信号注册多个函数,这些多个函数会按照注册的顺序执行