class documentation

class QueryEvents(event.Events):

View In Hierarchy

Represent events within the construction of a _query.Query object.

The _orm.QueryEvents hooks are now superseded by the _orm.SessionEvents.do_orm_execute event hook.

Class Method ​_listen Undocumented
Method before​_compile Receive the _query.Query object before it is composed into a core _expression.Select object.
Method before​_compile​_delete Allow modifications to the _query.Query object within _query.Query.delete.
Method before​_compile​_update Allow modifications to the _query.Query object within _query.Query.update.
Class Variable ​_target​_class​_doc Undocumented

Inherited from Events:

Class Method ​_accept​_with Undocumented
Class Method ​_clear Undocumented
Class Method ​_remove Undocumented
Static Method ​_set​_dispatch Undocumented
@classmethod
def _listen(cls, event_key, retval=False, bake_ok=False, **kw):

Undocumented

def before_compile(self, query):

Receive the _query.Query object before it is composed into a core _expression.Select object.

Deprecated since version 1.4: The _orm.QueryEvents.before_compile event is superseded by the much more capable _orm.SessionEvents.do_orm_execute hook. In version 1.4, the _orm.QueryEvents.before_compile event is no longer used for ORM-level attribute loads, such as loads of deferred or expired attributes as well as relationship loaders. See the new examples in :ref:`examples_session_orm_events` which illustrate new ways of intercepting and modifying ORM queries for the most common purpose of adding arbitrary filter criteria.

This event is intended to allow changes to the query given:

@event.listens_for(Query, "before_compile", retval=True)
def no_deleted(query):
    for desc in query.column_descriptions:
        if desc['type'] is User:
            entity = desc['entity']
            query = query.filter(entity.deleted == False)
    return query

The event should normally be listened with the retval=True parameter set, so that the modified query may be returned.

The .QueryEvents.before_compile event by default will disallow "baked" queries from caching a query, if the event hook returns a new _query.Query object. This affects both direct use of the baked query extension as well as its operation within lazy loaders and eager loaders for relationships. In order to re-establish the query being cached, apply the event adding the bake_ok flag:

@event.listens_for(
    Query, "before_compile", retval=True, bake_ok=True)
def my_event(query):
    for desc in query.column_descriptions:
        if desc['type'] is User:
            entity = desc['entity']
            query = query.filter(entity.deleted == False)
    return query

When bake_ok is set to True, the event hook will only be invoked once, and not called for subsequent invocations of a particular query that is being cached.

New in version 1.3.11: - added the "bake_ok" flag to the .QueryEvents.before_compile event and disallowed caching via the "baked" extension from occurring for event handlers that return a new _query.Query object if this flag is not set.

See Also

.QueryEvents.before_compile_update

.QueryEvents.before_compile_delete

:ref:`baked_with_before_compile`

def before_compile_delete(self, query, delete_context):

Allow modifications to the _query.Query object within _query.Query.delete.

Deprecated since version 1.4: The _orm.QueryEvents.before_compile_delete event is superseded by the much more capable _orm.SessionEvents.do_orm_execute hook.

Like the .QueryEvents.before_compile event, this event should be configured with retval=True, and the modified _query.Query object returned, as in

@event.listens_for(Query, "before_compile_delete", retval=True)
def no_deleted(query, delete_context):
    for desc in query.column_descriptions:
        if desc['type'] is User:
            entity = desc['entity']
            query = query.filter(entity.deleted == False)
    return query
New in version 1.2.17.

See Also

.QueryEvents.before_compile

.QueryEvents.before_compile_update

Parameters
querya _query.Query instance; this is also the .query attribute of the given "delete context" object.
delete​_contexta "delete context" object which is the same kind of object as described in :paramref:`.QueryEvents.after_bulk_delete.delete_context`.
def before_compile_update(self, query, update_context):

Allow modifications to the _query.Query object within _query.Query.update.

Deprecated since version 1.4: The _orm.QueryEvents.before_compile_update event is superseded by the much more capable _orm.SessionEvents.do_orm_execute hook.

Like the .QueryEvents.before_compile event, if the event is to be used to alter the _query.Query object, it should be configured with retval=True, and the modified _query.Query object returned, as in

@event.listens_for(Query, "before_compile_update", retval=True)
def no_deleted(query, update_context):
    for desc in query.column_descriptions:
        if desc['type'] is User:
            entity = desc['entity']
            query = query.filter(entity.deleted == False)

            update_context.values['timestamp'] = datetime.utcnow()
    return query

The .values dictionary of the "update context" object can also be modified in place as illustrated above.

New in version 1.2.17.

See Also

.QueryEvents.before_compile

.QueryEvents.before_compile_delete

Parameters
querya _query.Query instance; this is also the .query attribute of the given "update context" object.
update​_contextan "update context" object which is the same kind of object as described in :paramref:`.QueryEvents.after_bulk_update.update_context`. The object has a .values attribute in an UPDATE context which is the dictionary of parameters passed to _query.Query.update. This dictionary can be modified to alter the VALUES clause of the resulting UPDATE statement.
_target_class_doc: str =

Undocumented