Welcome to the Csdn-markdown Editor

Source: Internet
Author: User

Django Signals
1.什么是signal?    在特定事件发生时, 使用signal 发送者 能够通知一系列 接收者(一个或者多个).    python 本身没有类似机制的支持, 但是 django 基于 PyDispatcher 进行了性能 和代码结构上的优化来增加了signal功能.2.使用signal 2.1. 代码结构        ├── manage.py        ├── signalapp        │   ├── __init__.py        │   ├── models.py        │   ├── signals.py        │   ├── tests.py        │   ├── urls.py        │   ├── views.py        ├── signaltest        │   ├── __init__.py        │   ├── settings.py        │   ├── urls.py        │   ├── wsgi.py        └── yann 2.2. 注册signal
        import django.dispatch        save_done = django.dispatch.Signal(providing_args=[‘obj‘])
 2.3关联signal到对应的listener
     fromDjango.dbImportModelsImportSignals#引入文件     class article(models. Model):title = models. Charfield (max_length= $) content = models. TextField () Is_public = models. Booleanfield (default=True, blank=True) def send(self):Signals.save_done.send (Sender=article, obj=self)#关联 sender=article, which restricts the event to be triggered only when article has an update.          def __unicode__(self):            returnSelf.title def chufa_func(sender, **kwargs):            Print "SIGNAL"            if "obj" inchKwargs:obj = Kwargs.get ("obj") Signals.save_done.connect (chufa_func,sender=article)
   2.4触发监听事件
    article = Article(title="", content="")      article.send

Welcome to the Csdn-markdown Editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.