class registry(object):
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.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. |
_orm.registry
Parameters | |
metadata | An 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_registry | optional 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. |
constructor | Specify 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. |
_bind | Undocumented |
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
.
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
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.
See Also
_orm.clear_mappers
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.
Parameters | |
mapper | An optional callable, defaults to ~sqlalchemy.orm.mapper .
This function is used to generate new _orm.Mapper objects. |
cls | Defaults to object . A type to use as the base for the
generated declarative base class. May be a class or tuple of classes. |
name | Defaults to Base. The display name for the generated class. Customizing this is not required, but can improve clarity in tracebacks and debugging. |
metaclass | Defaults to .DeclarativeMeta . A metaclass or __metaclass__
compatible callable to use as the meta type of the generated
declarative base class. |
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 | |
cls | class to be mapped. |
Returns | |
a _orm.Mapper object. |
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_table | the _schema.Table or other
_sql.FromClause object that is the subject of the mapping.
Corresponds to the
:paramref:`_orm.mapper.local_table` parameter. |
**kw | all other keyword arguments are passed to the
_orm.mapper function directly. |
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 | |
cls | class to be mapped. |
Returns | |
the class that was passed. |