Django's built-in signal

Source: Internet
Author: User

Pre_init   automatically triggered before the model executes the construction method
Post_init Django model is automatically triggered after the construction method is executed
Pre_save Django objects are automatically triggered before they are saved
Post_save Django objects are automatically triggered after they are saved
Pre_delete This is a Django object that is automatically triggered before it is deleted
Post_delete Django objects are automatically triggered after they are deleted
M2m_changed automatically triggers before and after using the third sheet (add,remove,clear)
Class_prepared when the program starts, the table for the registered app, automatically triggers for each class

Management Signals
Pre_migrate automatically triggered before executing the migrate command
Post_migrate automatically triggers after executing the migrate command
Request/response  Signals
auto-Trigger before request_started request arrives
auto-Trigger after request_finished request ends


Test Signals
Settings_chnaged automatically triggers when a configuration file is tested using test
settings_rendered Using Test render template Yes, auto trigger

DATABSE Signals
Connections_created This is when the database is created, it automatically triggers


For Django's built-in signal, you only need to register the corresponding signal (register in __init__ under Project), and it will automatically trigger when the program executes.


Project_singal below the __init__ inside:
########################### #在这里面可以添加这个信号量

From django.db.models import signals
############################## #导入这个信号量的模块
Print (' operation to perform the semaphore of this Singal ')

########### #这个是一开始就会执行的操作
def before_save (*args,**kwargs):
Print (' Printing out ', Args,kwargs)
Signals.pre_save.connect (Before_save)
####################################################################################
Results of execution:
‘‘‘
() {' Signal ': <django.db.models.signals.modelsignal object at 0x0000022947a93080>, ' sender ': <class ' App01.models.Booke ', ' instance ': <booke:booke object (None), ' raw ': False, ' using ': ' Default ', ' Update_ Fields ': None}

‘‘‘
#//This will promise this log out


Views

From app01.models Import *

From django.shortcuts import HttpResponse

def  Action (requets):
Print (' Start executing this fun3 function ')
Models. User.objects.filter (id=1). Update (user_name= ' Yun ') # #这个是可以更新这个数据进去
Models. Book.objects.create (book_name= ' Jane Eyre ') ############## #这个是创建这个数据进去
Print (' Create successful ')
return HttpResponse (' Fun3 ')
Models
Class book (models. Model):
Book_name=models. Charfield (max_length=32,verbose_name= ' book name ')
Class  User (models. Model):
User_name=models. Username (MAX_LENGTH=32)


Django's built-in signal

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.