Warning

    For what I hope are obvious reasons, Peewee signals do not work when you use the , Model.update(), or methods. These methods generate queries that execute beyond the scope of the ORM, and the ORM does not know about which model instances might or might not be affected when the query executes.

    Signals work by hooking into the higher-level peewee APIs like Model.save() and , where the affected model instance is known ahead of time.

    The following signals are provided:

    pre_save

    Called immediately before an object is saved to the database. Provides an additional keyword argument , indicating whether the model is being saved for the first time or updated.

    post_save

    Called immediately after an object is saved to the database. Provides an additional keyword argument created, indicating whether the model is being saved for the first time or updated.

    Called immediately before an object is deleted from the database when Model.delete_instance() is used.

    post_delete

    Called immediately after an object is deleted from the database when is used.

    pre_init

    Called when a model class is first instantiated

    Whenever a signal is dispatched, it will call any handlers that have been registered. This allows totally separate code to respond to events like model save and delete.

    The class provides a method, which takes a callback function and two optional parameters for “sender” and “name”. If specified, the “sender” parameter should be a single model class and allows your callback to only receive signals from that one model class. The “name” parameter is used as a convenient alias in the event you wish to unregister your signal handler.

    Example usage:

    If you’d like, you can also use a decorator to connect signal handlers. This is functionally equivalent to the above example:

    Signal API

    class Signal

    Stores a list of receivers (callbacks) and calls them when the “send” method is invoked.

    • ([receiver=None[, name=None]])

      Disconnect the given receiver (or the receiver with the given name alias) so that it no longer is called. Either the receiver or the name must be provided.

    • send(instance, \args, **kwargs*)

      Parameters:instance – a model instance