The ORM includes a wide variety of hooks available for subscription. The event system in 0.7 is all new and supercedes the previous system of “extension” classes. For an introduction to the event API, see Events. Non-ORM events such as those regarding connections and low-level statement execution are described in Core Events.
Define events for object attributes.
These are typically defined on the class-bound descriptor for the target class.
e.g.:
from sqlalchemy import event
def my_append_listener(target, value, initiator):
print "received append event for target: %s" % target
event.listen(MyClass.collection, 'append', my_append_listener)
Listeners have the option to return a possibly modified version of the value, when the retval=True flag is passed to listen():
def validate_phone(target, value, oldvalue, initiator):
"Strip non-numeric characters from a phone number"
return re.sub(r'(?![0-9])', '', value)
# setup listener on UserContact.phone attribute, instructing
# it to use the return value
listen(UserContact.phone, 'set', validate_phone, retval=True)
A validation function like the above can also raise an exception such as ValueError to halt the operation.
Several modifiers are available to the listen() function.
Parameters: |
|
---|
Receive a collection append event.
Parameters: |
|
---|---|
Returns: | if the event was registered with retval=True, the given value, or a new effective value, should be returned. |
Receive a collection remove event.
Parameters: |
|
---|---|
Returns: | No return value is defined for this event. |
Receive a scalar set event.
Parameters: |
|
---|---|
Returns: | if the event was registered with retval=True, the given value, or a new effective value, should be returned. |
Define events specific to mappings.
e.g.:
from sqlalchemy import event
def my_before_insert_listener(mapper, connection, target):
# execute a stored procedure upon INSERT,
# apply the value to the row to be inserted
target.calculated_value = connection.scalar(
"select my_special_function(%d)"
% target.special_number)
# associate the listener function with SomeMappedClass,
# to execute during the "before_insert" hook
event.listen(SomeMappedClass, 'before_insert', my_before_insert_listener)
Available targets include mapped classes, instances of Mapper (i.e. returned by mapper(), class_mapper() and similar), as well as the Mapper class and mapper() function itself for global event reception:
from sqlalchemy.orm import mapper
def some_listener(mapper, connection, target):
log.debug("Instance %s being inserted" % target)
# attach to all mappers
event.listen(mapper, 'before_insert', some_listener)
Mapper events provide hooks into critical sections of the mapper, including those related to object instrumentation, object loading, and object persistence. In particular, the persistence methods before_insert(), and before_update() are popular places to augment the state being persisted - however, these methods operate with several significant restrictions. The user is encouraged to evaluate the SessionEvents.before_flush() and SessionEvents.after_flush() methods as more flexible and user-friendly hooks in which to apply additional database state during a flush.
When using MapperEvents, several modifiers are available to the event.listen() function.
Parameters: |
|
---|
Receive an object instance after a DELETE statement has been emitted corresponding to that instance.
This event is used to emit additional SQL statements on the given connection as well as to perform application specific bookkeeping related to a deletion event.
The event is often called for a batch of objects of the same class after their DELETE statements have been emitted at once in a previous step.
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive an object instance after an INSERT statement is emitted corresponding to that instance.
This event is used to modify in-Python-only state on the instance after an INSERT occurs, as well as to emit additional SQL statements on the given connection.
The event is often called for a batch of objects of the same class after their INSERT statements have been emitted at once in a previous step. In the extremely rare case that this is not desirable, the mapper() can be configured with batch=False, which will cause batches of instances to be broken up into individual (and more poorly performing) event->persist->event steps.
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive an object instance after an UPDATE statement is emitted corresponding to that instance.
This event is used to modify in-Python-only state on the instance after an UPDATE occurs, as well as to emit additional SQL statements on the given connection.
This method is called for all instances that are marked as “dirty”, even those which have no net changes to their column-based attributes, and for which no UPDATE statement has proceeded. An object is marked as dirty when any of its column-based attributes have a “set attribute” operation called or when any of its collections are modified. If, at update time, no column-based attributes have any net changes, no UPDATE statement will be issued. This means that an instance being sent to after_update() is not a guarantee that an UPDATE statement has been issued.
To detect if the column-based attributes on the object have net changes, and therefore resulted in an UPDATE statement, use object_session(instance).is_modified(instance, include_collections=False).
The event is often called for a batch of objects of the same class after their UPDATE statements have been emitted at once in a previous step. In the extremely rare case that this is not desirable, the mapper() can be configured with batch=False, which will cause batches of instances to be broken up into individual (and more poorly performing) event->persist->event steps.
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive an object instance before that instance is appended to a result list.
This is a rarely used hook which can be used to alter the construction of a result list returned by Query.
Parameters: |
|
---|---|
Returns: | If this method is registered with retval=True, a return value of EXT_STOP will prevent the instance from being appended to the given result list, whereas a return value of EXT_CONTINUE will result in the default behavior of appending the value to the result list. |
Receive an object instance before a DELETE statement is emitted corresponding to that instance.
This event is used to emit additional SQL statements on the given connection as well as to perform application specific bookkeeping related to a deletion event.
The event is often called for a batch of objects of the same class before their DELETE statements are emitted at once in a later step.
Handlers should not modify any attributes which are mapped by relationship(), nor should they attempt to make any modifications to the Session in this hook (including Session.add(), Session.delete(), etc.) - such changes will not take effect. For overall changes to the “flush plan”, use SessionEvents.before_flush().
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive an object instance before an INSERT statement is emitted corresponding to that instance.
This event is used to modify local, non-object related attributes on the instance before an INSERT occurs, as well as to emit additional SQL statements on the given connection.
The event is often called for a batch of objects of the same class before their INSERT statements are emitted at once in a later step. In the extremely rare case that this is not desirable, the mapper() can be configured with batch=False, which will cause batches of instances to be broken up into individual (and more poorly performing) event->persist->event steps.
Handlers should not modify any attributes which are mapped by relationship(), nor should they attempt to make any modifications to the Session in this hook (including Session.add(), Session.delete(), etc.) - such changes will not take effect. For overall changes to the “flush plan”, use SessionEvents.before_flush().
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive an object instance before an UPDATE statement is emitted corresponding to that instance.
This event is used to modify local, non-object related attributes on the instance before an UPDATE occurs, as well as to emit additional SQL statements on the given connection.
This method is called for all instances that are marked as “dirty”, even those which have no net changes to their column-based attributes. An object is marked as dirty when any of its column-based attributes have a “set attribute” operation called or when any of its collections are modified. If, at update time, no column-based attributes have any net changes, no UPDATE statement will be issued. This means that an instance being sent to before_update() is not a guarantee that an UPDATE statement will be issued, although you can affect the outcome here by modifying attributes so that a net change in value does exist.
To detect if the column-based attributes on the object have net changes, and will therefore generate an UPDATE statement, use object_session(instance).is_modified(instance, include_collections=False).
The event is often called for a batch of objects of the same class before their UPDATE statements are emitted at once in a later step. In the extremely rare case that this is not desirable, the mapper() can be configured with batch=False, which will cause batches of instances to be broken up into individual (and more poorly performing) event->persist->event steps.
Handlers should not modify any attributes which are mapped by relationship(), nor should they attempt to make any modifications to the Session in this hook (including Session.add(), Session.delete(), etc.) - such changes will not take effect. For overall changes to the “flush plan”, use SessionEvents.before_flush().
Parameters: |
|
---|---|
Returns: | No return value is supported by this event. |
Receive a row when a new object instance is about to be created from that row.
The method can choose to create the instance itself, or it can return EXT_CONTINUE to indicate normal object creation should take place. This listener is typically registered with retval=True.
Parameters: |
|
---|---|
Returns: | When configured with retval=True, the return value should be a newly created instance of the mapped class, or EXT_CONTINUE indicating that default object construction should take place. |
Receive a class when the mapper is first constructed, before instrumentation is applied to the mapped class.
This event is the earliest phase of mapper construction. Most attributes of the mapper are not yet initialized.
This listener can generally only be applied to the Mapper class overall.
Parameters: |
|
---|
Called when the mapper for the class is fully configured.
This event is the latest phase of mapper construction. The mapper should be in its final state.
Parameters: |
|
---|
Receive an instance before that instance has its attributes populated.
This usually corresponds to a newly loaded instance but may also correspond to an already-loaded instance which has unloaded attributes to be populated. The method may be called many times for a single instance, as multiple result rows are used to populate eagerly loaded collections.
Most usages of this hook are obsolete. For a generic “object has been newly created from a row” hook, use InstanceEvents.load().
Parameters: |
|
---|---|
Returns: | When configured with retval=True, a return value of EXT_STOP will bypass instance population by the mapper. A value of EXT_CONTINUE indicates that default instance population should take place. |
Perform pre-processing on the given result row and return a new row instance.
This listener is typically registered with retval=True. It is called when the mapper first receives a row, before the object identity or the instance itself has been derived from that row. The given row may or may not be a RowProxy object - it will always be a dictionary-like object which contains mapped columns as keys. The returned object should also be a dictionary-like object which recognizes mapped columns as keys.
Parameters: |
|
---|---|
Returns: | When configured with retval=True, the function should return a dictionary-like row object, or EXT_CONTINUE, indicating the original row should be used. |
Define events specific to object lifecycle.
e.g.:
from sqlalchemy import event
def my_load_listener(target, context):
print "on load!"
event.listen(SomeMappedClass, 'load', my_load_listener)
Available targets include mapped classes, instances of Mapper (i.e. returned by mapper(), class_mapper() and similar), as well as the Mapper class and mapper() function itself for global event reception:
from sqlalchemy.orm import mapper
def some_listener(target, context):
log.debug("Instance %s being loaded" % target)
# attach to all mappers
event.listen(mapper, 'load', some_listener)
Instance events are closely related to mapper events, but are more specific to the instance and its instrumentation, rather than its system of persistence.
When using InstanceEvents, several modifiers are available to the event.listen() function.
Parameters: |
|
---|
Receive an object instance after its attributes or some subset have been expired.
‘keys’ is a list of attribute names. If None, the entire state was expired.
Parameters: |
|
---|
Called when the first instance of a particular mapping is called.
Receive an instance when it’s constructor is called.
This method is only called during a userland construction of an object. It is not called when an object is loaded from the database.
Receive an instance when it’s constructor has been called, and raised an exception.
This method is only called during a userland construction of an object. It is not called when an object is loaded from the database.
Receive an object instance after it has been created via __new__, and after initial attribute population has occurred.
This typically occurs when the instance is created based on incoming result rows, and is only called once for that instance’s lifetime.
Note that during a result-row load, this method is called upon the first row received for this instance. Note that some attributes and collections may or may not be loaded or even initialized, depending on what’s present in the result rows.
Parameters: |
|
---|
Receive an object instance when its associated state is being pickled.
Parameters: |
|
---|
Receive an object instance after one or more attributes have been refreshed from a query.
Parameters: |
|
---|
Receive an object instance as it is ‘resurrected’ from garbage collection, which occurs when a “dirty” state falls out of scope.
Parameters: | target – the mapped instance. If the event is configured with raw=True, this will instead be the InstanceState state-management object associated with the instance. |
---|
Receive an object instance after it’s associated state has been unpickled.
Parameters: |
|
---|
Define events specific to Session lifecycle.
e.g.:
from sqlalchemy import event
from sqlalchemy.orm import sessionmaker
def my_before_commit(session):
print "before commit!"
Session = sessionmaker()
event.listen(Session, "before_commit", my_before_commit)
The listen() function will accept Session objects as well as the return result of sessionmaker() and scoped_session().
Additionally, it accepts the Session class which will apply listeners to all Session instances globally.
Execute after an instance is attached to a session.
This is called after an add, delete or merge.
Execute after a transaction is begun on a connection
transaction is the SessionTransaction. This method is called after an engine level transaction is begun on a connection.
Execute after a bulk delete operation to the session.
This is called after a session.query(...).delete()
query is the query object that this delete operation was called on. query_context was the query context object. result is the result object returned from the bulk operation.
Execute after a bulk update operation to the session.
This is called after a session.query(...).update()
query is the query object that this update operation was called on. query_context was the query context object. result is the result object returned from the bulk operation.
Execute after a commit has occurred.
Note that this may not be per-flush if a longer running transaction is ongoing.
Execute after flush has completed, but before commit has been called.
Note that the session’s state is still in pre-flush, i.e. ‘new’, ‘dirty’, and ‘deleted’ lists still show pre-flush state as well as the history settings on instance attributes.
Execute after flush has completed, and after the post-exec state occurs.
This will be when the ‘new’, ‘dirty’, and ‘deleted’ lists are in their final state. An actual commit() may or may not have occurred, depending on whether or not the flush started its own transaction or participated in a larger transaction.
Execute after a rollback has occurred.
Note that this may not be per-flush if a longer running transaction is ongoing.
Execute before commit is called.
Note that this may not be per-flush if a longer running transaction is ongoing.
Execute before flush process has started.
instances is an optional list of objects which were passed to the flush() method.
Events related to class instrumentation events.
The listeners here support being established against any new style class, that is any object that is a subclass of ‘type’. Events will then be fired off for events against that class as well as all subclasses. ‘type’ itself is also accepted as a target in which case the events fire for all classes.
Called when an attribute is instrumented.
Called after the given class is instrumented.
To get at the ClassManager, use manager_of_class().
Called before the given class is uninstrumented.
To get at the ClassManager, use manager_of_class().
User-defined class instrumentation extension.
InstrumentationManager can be subclassed in order to change how class instrumentation proceeds. This class exists for the purposes of integration with other object management frameworks which would like to entirely modify the instrumentation methodology of the ORM, and is not intended for regular usage. For interception of class instrumentation events, see InstrumentationEvents.
For an example of InstrumentationManager, see the example Attribute Instrumentation.
The API for this class should be considered as semi-stable, and may change slightly with new releases.