"""
在web开发中, 你可能会遇到下面这种场景:
在用户完成某个操作后, 自动去执行一些后续的操作. 譬如用户完成修改密码后,你要发送一份确认邮件 观察者模式:观察者模式(Observer)完美的将观察者和被观察的对象分离开。
举个例子,用户界面可以作为一个观察者,业务数据是被观察者,
用户界面观察业务数据的变化,发现数据变化后,就显示在界面上
""" """学习网站https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html""" """两个重要的对象:Sender receiver
sender dispatch a signal, receiver receive signal and do something
sender 是一个object, receiver是一个方法,或者对象的方法
将sender和receiver关联起来是通过signal dispatchers, 即Signal.connect()方法来关联sender,receiver
""" # post_save信号
""" 在mode对象执行save函数时,就会触发该信号""" from django.contrib.auth.models import User
from django.db.models.signals import post_save def save_profile(sender, instance, **kwargs):
instance.profile.save() post_save.connect(save_profile, sender=User) # save_profile是receiver,User是sender, post_save是signal, 上面代码可以理解成:当User对象执行save函数后,就会执行save_profile函数 # 如果去掉post_save.connect的sender参数,如: post_save.connect(save_profile), 则就是任意一个model执行了save函数,都会调用save_profile函数 """关联sender和receiver还可以通过@receiver装饰器来完成 def receiver(signal, **kwargs)"""
# 两个参数,signal可以是元组或者列表,或者单个信号
# **kwargs是位置参数,注意 from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver @receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save() # 注册信号不应该在app的models或者根目录,django建议我们将signals放到config里面,以下面为了,profile为一个app;cmdbox是project # profile/signals.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver from cmdbox.profiles.models import Profile @receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance) @receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save() """ instance参数就是sender的实例化对象""" #profiles/app.py:
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _ class ProfilesConfig(AppConfig):
name = 'cmdbox.profiles'
verbose_name = _('profiles') def ready(self):
import cmdbox.profiles.signals # noqa # profiles/__init__.py: 如果已经在工程里面注册了app,这里是不需要的
default_app_config = 'cmdbox.profiles.apps.ProfilesConfig' # 如果是通过connect关联sender和receiver,参考下面代码 # profile/signals.py
from cmdbox.profiles.models import Profile def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance) def save_user_profile(sender, instance, **kwargs):
instance.profile.save() # profile/app.py
from django.apps import AppConfig
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _ from cmdbox.profiles.signals import create_user_profile, save_user_profile class ProfilesConfig(AppConfig):
name = 'cmdbox.profiles'
verbose_name = _('profiles') def ready(self):
post_save.connect(create_user_profile, sender=User)
post_save.connect(save_user_profile, sender=User) # profiles/__init__.py:
default_app_config = 'cmdbox.profiles.apps.ProfilesConfig'
 

[ecmagnet][django] 如何使用django的signal的更多相关文章

  1. 第六章:Django 综合篇 - 8:信号 signal

    django自带一套信号机制来帮助我们在框架的不同位置之间传递信息.也就是说,当某一事件发生时,信号系统可以允许一个或多个发送者(senders)将通知或信号(signals)发送给一组接受者(rec ...

  2. Django组件(五) Django之ContentType组件

    基础使用 -contenttype组件 -django提供的一个快速连表操作的组件,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中. 当我们的项目做数据迁移后,会 ...

  3. 五步教你实现使用Nginx+uWSGI+Django方法部署Django程序

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  4. 【Django】Django 如何使用 Django设置的日志?

    代码: from django.core.management.base import BaseCommand, CommandError from django.db import models # ...

  5. python学习笔记--Django入门三 Django 与数据库的交互:数据建模

    把数据存取逻辑.业务逻辑和表现逻辑组合在一起的概念有时被称为软件架构的 Model-View-Controller (MVC)模式.在这个模式中, Model 代表数据存取层,View 代表的是系统中 ...

  6. [Django高级]理解django中的中间件机制和执行顺序

    原文来自 Understanding Django Middlewares, 这篇文章从整体上介绍了django中中间件定义,作用,和怎么样自己写中间件 –orangleliu. 注:middlewa ...

  7. Python Django 1.Hello Django

    #安装Djangopip install Django #==版本号#选择路径:D:#任意文件夹名 cd Django #罗列Django所提供的命令,其中startproject命令来创建项目 dj ...

  8. Django学习之django自带的contentType表 GenericRelation GenericForeignKey

    Django学习之django自带的contentType表   通过django的contentType表来搞定一个表里面有多个外键的简单处理: 摘自:https://blog.csdn.net/a ...

  9. Django项目和Django初体验和创建、目录结构认识

    .MVC的设计方式(跟Flask一样,都是MVC的设计模式) .开发效率高 .功能强大(丰富的第三方组件) .安全性高(帮助开发者规避安全漏洞) 目前市面上使用:Django>Flask #使用 ...

  10. Django框架 (七) Django ORM模型

    ORM简介 查询数据层次图解:如果操作mysql,ORM是在pymysq之上又进行了一层封装

随机推荐

  1. EF6.0 对于数据库优 模式 新加功能

    EF6.0相对于5.0新加了很多功能.先看看两个模式的一些特点. 数据库优先(设计者)和代码优先两者的特点: 连接弹性 异步查询和保存 基于代码的配置 数据库命令记录 数据库命令截取 依赖决议 DbS ...

  2. Java实现双向冒泡排序

    public class BubbleSort_Two { public static void bubbleSort_Two(int[] list){ //j在最外层定义 boolean needN ...

  3. 网页后缀html、htm、shtml、shtm有什么区别?

    每一个网页或者说是web页都有其固定的后缀名,不同的后缀名对应着不同的文件格式和不同的规则.协议.用法,最常见的web页的后缀名是.html和.htm,但这只是web页最基本的两种文件格式,今天我们来 ...

  4. 自动计算UITableViewCell高度2(CGRect约束)

    1.先创建model .h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface LBDNe ...

  5. c++cmb

    #include<windows.h> #include<bits/stdc++.h> using namespace std; ]; int main() { printf( ...

  6. 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士

    基环外向树dp竟然如此简单…… Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发 ...

  7. poj_2084_Game of Connections

    This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . . . , 2n - 1 ...

  8. 蓝图(Blueprint)详解

    Blueprint 模块化 随着flask程序越来越复杂,我们需要对程序进行模块化的处理,针对一个简单的flask程序进行模块化处理 举例来说: 我们有一个博客程序,前台界面需要的路由为:首页,列表, ...

  9. Percona-Tookit工具包之pt-table-checksum

      Preface       The master-slave replication is commonly used in our product evironment.On account o ...

  10. MySQL的日志相关内容

    本篇文章介绍一下mysql的备份和日志,由于备份时需要用到日志,所以在讲备份前,如果日志内容篇幅过长,将会把日志和备份分开单独来讲,先简单介绍一下mysql的日志相关内容. MySQL日志 日志是my ...