class documentation

class registry(object):

View In Hierarchy

Generalized registry for mapping classes.

The _orm.registry serves as the basis for maintaining a collection of mappings, and provides configurational hooks used to map classes.

The three general kinds of mappings supported are Declarative Base, Declarative Decorator, and Imperative Mapping. All of these mapping styles may be used interchangeably:

  • _orm.registry.generate_base returns a new declarative base class, and is the underlying implementation of the _orm.declarative_base function.
  • _orm.registry.mapped provides a class decorator that will apply declarative mapping to a class without the use of a declarative base class.
  • _orm.registry.map_imperatively will produce a _orm.Mapper for a class without scanning the class for declarative class attributes. This method suits the use case historically provided by the _orm.mapper classical mapping function.
New in version 1.4.

See Also

:ref:`orm_mapping_classes_toplevel` - overview of class mapping styles.

Class Method ​_recurse​_with​_dependencies Undocumented
Class Method ​_recurse​_with​_dependents Undocumented
Method __init__ Construct a new _orm.registry
Method ​_add​_manager Undocumented
Method ​_add​_non​_primary​_mapper Undocumented
Method ​_dispose​_cls Undocumented
Method ​_dispose​_manager​_and​_mapper Undocumented
Method ​_flag​_new​_mapper Undocumented
Method ​_mappers​_to​_configure Undocumented
Method ​_set​_depends​_on Undocumented
Method as​_declarative​_base Class decorator which will invoke _orm.registry.generate_base for a given base class.
Method configure Configure all as-yet unconfigured mappers in this _orm.registry.
Method dispose Dispose of all mappers in this _orm.registry.
Method generate​_base Generate a declarative base class.
Method map​_declaratively Map a class declaratively.
Method map​_imperatively Map a class imperatively.
Method mapped Class decorator that will apply the Declarative mapping process to a given class.
Instance Variable ​_class​_registry Undocumented
Instance Variable ​_dependencies Undocumented
Instance Variable ​_dependents Undocumented
Instance Variable ​_managers Undocumented
Instance Variable ​_new​_mappers Undocumented
Instance Variable ​_non​_primary​_mappers Undocumented
Instance Variable constructor Undocumented
Instance Variable metadata Undocumented
Property mappers read only collection of all _orm.Mapper objects.
@classmethod
def _recurse_with_dependencies(cls, registries):

Undocumented

@classmethod
def _recurse_with_dependents(cls, registries):

Undocumented

def __init__(self, metadata=None, class_registry=None, constructor=_declarative_constructor, _bind=None):
Construct a new _orm.registry
Parameters
metadataAn optional _schema.MetaData instance. All _schema.Table objects generated using declarative table mapping will make use of this _schema.MetaData collection. If this argument is left at its default of None, a blank _schema.MetaData collection is created.
class​_registryoptional dictionary that will serve as the registry of class names-> mapped classes when string names are used to identify classes inside of _orm.relationship and others. Allows two or more declarative base classes to share the same registry of class names for simplified inter-base relationships.
constructorSpecify the implementation for the __init__ function on a mapped class that has no __init__ of its own. Defaults to an implementation that assigns **kwargs for declared fields and relationships to an instance. If None is supplied, no __init__ will be provided and construction will fall back to cls.__init__ by way of the normal Python semantics.
​_bindUndocumented
def _add_manager(self, manager):

Undocumented

def _add_non_primary_mapper(self, np_mapper):

Undocumented

def _dispose_cls(self, cls):

Undocumented

def _dispose_manager_and_mapper(self, manager):

Undocumented

def _flag_new_mapper(self, mapper):

Undocumented

def _mappers_to_configure(self):

Undocumented

def _set_depends_on(self, registry):

Undocumented

def as_declarative_base(self, **kw):

Class decorator which will invoke _orm.registry.generate_base for a given base class.

E.g.:

from sqlalchemy.orm import registry

mapper_registry = registry()

@mapper_registry.as_declarative_base()
class Base(object):
    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()
    id = Column(Integer, primary_key=True)

class MyMappedClass(Base):
    # ...

All keyword arguments passed to _orm.registry.as_declarative_base are passed along to _orm.registry.generate_base.

def configure(self, cascade=False):

Configure all as-yet unconfigured mappers in this _orm.registry.

The configure step is used to reconcile and initialize the _orm.relationship linkages between mapped classes, as well as to invoke configuration events such as the _orm.MapperEvents.before_configured and _orm.MapperEvents.after_configured, which may be used by ORM extensions or user-defined extension hooks.

If one or more mappers in this registry contain _orm.relationship constructs that refer to mapped classes in other registries, this registry is said to be dependent on those registries. In order to configure those dependent registries automatically, the :paramref:`_orm.registry.configure.cascade` flag should be set to True. Otherwise, if they are not configured, an exception will be raised. The rationale behind this behavior is to allow an application to programmatically invoke configuration of registries while controlling whether or not the process implicitly reaches other registries.

As an alternative to invoking _orm.registry.configure, the ORM function _orm.configure_mappers function may be used to ensure configuration is complete for all _orm.registry objects in memory. This is generally simpler to use and also predates the usage of _orm.registry objects overall. However, this function will impact all mappings throughout the running Python process and may be more memory/time consuming for an application that has many registries in use for different purposes that may not be needed immediately.

See Also

_orm.configure_mappers

New in version 1.4.0b2.
def dispose(self, cascade=False):

Dispose of all mappers in this _orm.registry.

After invocation, all the classes that were mapped within this registry will no longer have class instrumentation associated with them. This method is the per-_orm.registry analogue to the application-wide _orm.clear_mappers function.

If this registry contains mappers that are dependencies of other registries, typically via _orm.relationship links, then those registries must be disposed as well. When such registries exist in relation to this one, their _orm.registry.dispose method will also be called, if the :paramref:`_orm.registry.dispose.cascade` flag is set to True; otherwise, an error is raised if those registries were not already disposed.

New in version 1.4.0b2.

See Also

_orm.clear_mappers

def generate_base(self, mapper=None, cls=object, name='Base', metaclass=DeclarativeMeta):

Generate a declarative base class.

Classes that inherit from the returned class object will be automatically mapped using declarative mapping.

E.g.:

from sqlalchemy.orm import registry

mapper_registry = registry()

Base = mapper_registry.generate_base()

class MyClass(Base):
    __tablename__ = "my_table"
    id = Column(Integer, primary_key=True)

The above dynamically generated class is equivalent to the non-dynamic example below:

from sqlalchemy.orm import registry
from sqlalchemy.orm.decl_api import DeclarativeMeta

mapper_registry = registry()

class Base(metaclass=DeclarativeMeta):
    __abstract__ = True
    registry = mapper_registry
    metadata = mapper_registry.metadata

    __init__ = mapper_registry.constructor

The _orm.registry.generate_base method provides the implementation for the _orm.declarative_base function, which creates the _orm.registry and base class all at once.

See the section :ref:`orm_declarative_mapping` for background and examples.

See Also

:ref:`orm_declarative_mapping`

_orm.declarative_base

Parameters
mapperAn optional callable, defaults to ~sqlalchemy.orm.mapper. This function is used to generate new _orm.Mapper objects.
clsDefaults to object. A type to use as the base for the generated declarative base class. May be a class or tuple of classes.
nameDefaults to Base. The display name for the generated class. Customizing this is not required, but can improve clarity in tracebacks and debugging.
metaclassDefaults to .DeclarativeMeta. A metaclass or __metaclass__ compatible callable to use as the meta type of the generated declarative base class.
def map_declaratively(self, cls):

Map a class declaratively.

In this form of mapping, the class is scanned for mapping information, including for columns to be associated with a table, and/or an actual table object.

Returns the _orm.Mapper object.

E.g.:

from sqlalchemy.orm import registry

mapper_registry = registry()

class Foo:
    __tablename__ = 'some_table'

    id = Column(Integer, primary_key=True)
    name = Column(String)

mapper = mapper_registry.map_declaratively(Foo)

This function is more conveniently invoked indirectly via either the _orm.registry.mapped class decorator or by subclassing a declarative metaclass generated from _orm.registry.generate_base.

See the section :ref:`orm_declarative_mapping` for complete details and examples.

See Also

:ref:`orm_declarative_mapping`

_orm.registry.mapped - more common decorator interface to this function.

_orm.registry.map_imperatively

Parameters
clsclass to be mapped.
Returns
a _orm.Mapper object.
def map_imperatively(self, class_, local_table=None, **kw):

Map a class imperatively.

In this form of mapping, the class is not scanned for any mapping information. Instead, all mapping constructs are passed as arguments.

This method is intended to be fully equivalent to the classic SQLAlchemy _orm.mapper function, except that it's in terms of a particular registry.

E.g.:

from sqlalchemy.orm import registry

mapper_registry = registry()

my_table = Table(
    "my_table",
    mapper_registry.metadata,
    Column('id', Integer, primary_key=True)
)

class MyClass:
    pass

mapper_registry.map_imperatively(MyClass, my_table)

See the section :ref:`orm_imperative_mapping` for complete background and usage examples.

Parameters
class​_The class to be mapped. Corresponds to the :paramref:`_orm.mapper.class_` parameter.
local​_tablethe _schema.Table or other _sql.FromClause object that is the subject of the mapping. Corresponds to the :paramref:`_orm.mapper.local_table` parameter.
**kwall other keyword arguments are passed to the _orm.mapper function directly.
def mapped(self, cls):

Class decorator that will apply the Declarative mapping process to a given class.

E.g.:

from sqlalchemy.orm import registry

mapper_registry = registry()

@mapper_registry.mapped
class Foo:
    __tablename__ = 'some_table'

    id = Column(Integer, primary_key=True)
    name = Column(String)

See the section :ref:`orm_declarative_mapping` for complete details and examples.

See Also

:ref:`orm_declarative_mapping`

_orm.registry.generate_base - generates a base class that will apply Declarative mapping to subclasses automatically using a Python metaclass.

Parameters
clsclass to be mapped.
Returns
the class that was passed.
_class_registry =

Undocumented

_dependencies: set =

Undocumented

_dependents: set =

Undocumented

_managers =

Undocumented

_new_mappers: bool =

Undocumented

_non_primary_mappers =

Undocumented

constructor =

Undocumented

metadata =

Undocumented

@property
mappers =
read only collection of all _orm.Mapper objects.