class documentation

Represent events within the construction of a :class:`_query.Query` object. .. legacy:: The :class:`_orm.QueryEvents` event methods are legacy as of SQLAlchemy 2.0, and only apply to direct use of the :class:`_orm.Query` object. They are not used for :term:`2.0 style` statements. For events to intercept and modify 2.0 style ORM use, use the :meth:`_orm.SessionEvents.do_orm_execute` hook. The :class:`_orm.QueryEvents` hooks are now superseded by the :meth:`_orm.SessionEvents.do_orm_execute` event hook.

Method before_compile Receive the :class:`_query.Query` object before it is composed into a core :class:`_expression.Select` object.
Method before_compile_delete Allow modifications to the :class:`_query.Query` object within :meth:`_query.Query.delete`.
Method before_compile_update Allow modifications to the :class:`_query.Query` object within :meth:`_query.Query.update`.
Class Method _listen Undocumented
Class Variable _target_class_doc Undocumented

Inherited from Events:

Class Method _accept_with Undocumented
Class Method _clear Undocumented
Class Method _remove Undocumented

Inherited from _HasEventsDispatch (via Events):

Method __getattr__ Undocumented
Method __init_subclass__ Intercept new Event subclasses and create associated _Dispatch classes.
Class Variable dispatch reference back to the _Dispatch class.
Class Method _create_dispatcher_class Create a :class:`._Dispatch` class corresponding to an :class:`.Events` class.
Static Method _set_dispatch Undocumented
Class Variable _dispatch_target class which will receive the .dispatch collection
def before_compile(self, query: Query[Any]): (source)

Receive the :class:`_query.Query` object before it is composed into a core :class:`_expression.Select` object. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile` event is superseded by the much more capable :meth:`_orm.SessionEvents.do_orm_execute` hook. In version 1.4, the :meth:`_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 :meth:`.QueryEvents.before_compile` event by default will disallow "baked" queries from caching a query, if the event hook returns a new :class:`_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. .. versionadded:: 1.3.11 - added the "bake_ok" flag to the :meth:`.QueryEvents.before_compile` event and disallowed caching via the "baked" extension from occurring for event handlers that return a new :class:`_query.Query` object if this flag is not set. .. seealso:: :meth:`.QueryEvents.before_compile_update` :meth:`.QueryEvents.before_compile_delete` :ref:`baked_with_before_compile`

def before_compile_delete(self, query: Query[Any], delete_context: BulkDelete): (source)

Allow modifications to the :class:`_query.Query` object within :meth:`_query.Query.delete`. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile_delete` event is superseded by the much more capable :meth:`_orm.SessionEvents.do_orm_execute` hook. Like the :meth:`.QueryEvents.before_compile` event, this event should be configured with ``retval=True``, and the modified :class:`_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 :param query: a :class:`_query.Query` instance; this is also the ``.query`` attribute of the given "delete context" object. :param delete_context: a "delete context" object which is the same kind of object as described in :paramref:`.QueryEvents.after_bulk_delete.delete_context`. .. versionadded:: 1.2.17 .. seealso:: :meth:`.QueryEvents.before_compile` :meth:`.QueryEvents.before_compile_update`

def before_compile_update(self, query: Query[Any], update_context: BulkUpdate): (source)

Allow modifications to the :class:`_query.Query` object within :meth:`_query.Query.update`. .. deprecated:: 1.4 The :meth:`_orm.QueryEvents.before_compile_update` event is superseded by the much more capable :meth:`_orm.SessionEvents.do_orm_execute` hook. Like the :meth:`.QueryEvents.before_compile` event, if the event is to be used to alter the :class:`_query.Query` object, it should be configured with ``retval=True``, and the modified :class:`_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. :param query: a :class:`_query.Query` instance; this is also the ``.query`` attribute of the given "update context" object. :param update_context: an "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 :meth:`_query.Query.update`. This dictionary can be modified to alter the VALUES clause of the resulting UPDATE statement. .. versionadded:: 1.2.17 .. seealso:: :meth:`.QueryEvents.before_compile` :meth:`.QueryEvents.before_compile_delete`

@classmethod
def _listen(cls, event_key: _EventKey[_ET], retval: bool = False, bake_ok: bool = False, **kw: Any): (source)

Undocumented

_target_class_doc: str = (source)

Undocumented