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.