Called right before a specific mapper is to be configured. The :meth:`.MapperEvents.before_mapper_configured` event is invoked for each mapper that is encountered when the :func:`_orm.configure_mappers` function proceeds through the current list of not-yet-configured
(
self, mapper: Mapper[_O], class_: Type[_O]
)
| 955 | |
| 956 | @event._omit_standard_example |
| 957 | def before_mapper_configured( |
| 958 | self, mapper: Mapper[_O], class_: Type[_O] |
| 959 | ) -> None: |
| 960 | """Called right before a specific mapper is to be configured. |
| 961 | |
| 962 | The :meth:`.MapperEvents.before_mapper_configured` event is invoked |
| 963 | for each mapper that is encountered when the |
| 964 | :func:`_orm.configure_mappers` function proceeds through the current |
| 965 | list of not-yet-configured mappers. It is similar to the |
| 966 | :meth:`.MapperEvents.mapper_configured` event, except that it's invoked |
| 967 | right before the configuration occurs, rather than afterwards. |
| 968 | |
| 969 | The :meth:`.MapperEvents.before_mapper_configured` event includes |
| 970 | the special capability where it can force the configure step for a |
| 971 | specific mapper to be skipped; to use this feature, establish |
| 972 | the event using the ``retval=True`` parameter and return |
| 973 | the :attr:`.orm.interfaces.EXT_SKIP` symbol to indicate the mapper |
| 974 | should be left unconfigured:: |
| 975 | |
| 976 | from sqlalchemy import event |
| 977 | from sqlalchemy.orm import EXT_SKIP |
| 978 | from sqlalchemy.orm import DeclarativeBase |
| 979 | |
| 980 | |
| 981 | class DontConfigureBase(DeclarativeBase): |
| 982 | pass |
| 983 | |
| 984 | |
| 985 | @event.listens_for( |
| 986 | DontConfigureBase, |
| 987 | "before_mapper_configured", |
| 988 | # support return values for the event |
| 989 | retval=True, |
| 990 | # propagate the listener to all subclasses of |
| 991 | # DontConfigureBase |
| 992 | propagate=True, |
| 993 | ) |
| 994 | def dont_configure(mapper, cls): |
| 995 | return EXT_SKIP |
| 996 | |
| 997 | .. seealso:: |
| 998 | |
| 999 | :meth:`.MapperEvents.before_configured` |
| 1000 | |
| 1001 | :meth:`.MapperEvents.after_configured` |
| 1002 | |
| 1003 | :meth:`.RegistryEvents.before_configured` |
| 1004 | |
| 1005 | :meth:`.RegistryEvents.after_configured` |
| 1006 | |
| 1007 | :meth:`.MapperEvents.mapper_configured` |
| 1008 | |
| 1009 | """ |
| 1010 | |
| 1011 | def mapper_configured(self, mapper: Mapper[_O], class_: Type[_O]) -> None: |
| 1012 | r"""Called when a specific mapper has completed its own configuration |
no outgoing calls