Middleware engine tutorial (5) Signal

Source: Internet
Author: User
The consumer engine sends some signals when performing data operations. we can connect these signals for some additional operations. Note: To use signals in the engine, you need to install the library blinker. $ Pipinstallblinker1_engine provides the following signal: pre_init: Create a new Document or Embedded

The consumer engine sends some signals when performing data operations. we can connect these signals for some additional operations. Note: To use signals in the engine, you need to install the library blinker. $ Pip install blinker internal engine provides the following signal: pre_init: Create a new Document or Embedded

The consumer engine sends some signals when performing data operations. we can connect these signals for some additional operations. Note: To use signals in the engine, you must installBlinkerThis library.

$ pip install blinker

The signal provided by consumer engine is as follows:

  • Pre_init: it is called after a new Document or EmbeddedDocument instance object is created and before the object is initialized.
  • Post_init: called after the Document or EmbeddedDocument instance object initialization is complete.
  • Pre_save: called before the save method is executed.
  • Pre_save_post_validation: called before data is saved after data validation is complete.
  • Post_save: called after the data is saved.
  • Pre_delete: called before the delete method is executed.
  • Post_delete: called after the record is successfully deleted.
  • Pre_bulk_insert: called before data insertion after data validation.
  • Post_bulk_insert: called after successful data insertion.
Event connection

Use signals to connect the signal to the callback function.

from mongoengine import *from mongoengine import signalsclass Author(Document):    name = StringField()    @classmethod    def pre_save(cls, sender, document, **kwargs):        print("Pre Save: %s" % document.name)    @classmethod    def post_save(cls, sender, document, **kwargs):        print("Post Save: %s" % document.name)        if 'created' in kwargs:            if kwargs['created']:                print("Created")            else:                print("Updated")signals.pre_save.connect(Author.pre_save, sender=Author)signals.post_save.connect(Author.post_save, sender=Author)

Note: For RefereneFieldReverse_delete_rulesParameters do not trigger signals.

Original article address: audio engine tutorial (5)-signal, thanks to the original author for sharing.

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.