2.7. Signals

New in version 0.5.


Signal support is provided by the excellent blinker library. If you wish to enable signal support this library must be installed, though it is not required for MongoEngine to function.

2.7.1. Overview

Signals are found within the mongoengine.signals module. Unless specified signals receive no additional arguments beyond the sender class and document instance. Post-signals are only called if there were no exceptions raised during the processing of their related function.

Available signals include:

Called during the creation of a new Document or EmbeddedDocument instance, after the constructor arguments have been collected but before any additional processing has been done to them. (I.e. assignment of default values.) Handlers for this signal are passed the dictionary of arguments using the values keyword argument and may modify this dictionary prior to returning.
Called after all processing of a new Document or EmbeddedDocument instance has been completed.
Called within save() prior to performing any actions.
Called within save() after validation has taken place but before saving.
Called within save() after all actions (validation, insert/update, cascades, clearing dirty flags) have completed successfully. Passed the additional boolean keyword argument created to indicate if the save was an insert or an update.
Called within delete() prior to attempting the delete operation.
Called within delete() upon successful deletion of the record.
Called after validation of the documents to insert, but prior to any data being written. In this case, the document argument is replaced by a documents argument representing the list of documents being inserted.
Called after a successful bulk insert operation. As per pre_bulk_insert, the document argument is omitted and replaced with a documents argument. An additional boolean argument, loaded, identifies the contents of documents as either Document instances when True or simply a list of primary key values for the inserted records if False.

2.7.2. Attaching Events

After writing a handler function like the following:

import logging
from datetime import datetime

from mongoengine import *
from mongoengine import signals

def update_modified(sender, document):
    document.modified = datetime.utcnow()

You attach the event handler to your Document or EmbeddedDocument subclass:

class Record(Document):
    modified = DateTimeField()


While this is not the most elaborate document model, it does demonstrate the concepts involved. As a more complete demonstration you can also define your handlers within your subclass:

class Author(Document):
    name = StringField()

    def pre_save(cls, sender, document, **kwargs):
        logging.debug("Pre Save: %s" % document.name)

    def post_save(cls, sender, document, **kwargs):
        logging.debug("Post Save: %s" % document.name)
        if 'created' in kwargs:
            if kwargs['created']:

signals.pre_save.connect(Author.pre_save, sender=Author)
signals.post_save.connect(Author.post_save, sender=Author)

Finally, you can also use this small decorator to quickly create a number of signals and attach them to your Document or EmbeddedDocument subclasses as class decorators:

def handler(event):
    """Signal decorator to allow use of callback functions as class decorators."""

    def decorator(fn):
        def apply(cls):
            event.connect(fn, sender=cls)
            return cls

        fn.apply = apply
        return fn

    return decorator

Using the first example of updating a modification time the code is now much cleaner looking while still allowing manual execution of the callback:

def update_modified(sender, document):
    document.modified = datetime.utcnow()

class Record(Document):
    modified = DateTimeField()

2.7.3. ReferenceFields and Signals

Currently reverse_delete_rule does not trigger signals on the other part of the relationship. If this is required you must manually handle the reverse deletion.

Read the Docs v: latest
On Read the Docs
Project Home

Free document hosting provided by Read the Docs.