diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-03-29 14:41:41 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-03-29 14:41:41 +0000 |
commit | 99ed3922678ac050888dad7ca1c2a2195d65e2c0 (patch) | |
tree | f2ce3103168be46e1563dd1a865c229e09ff8d3a /lib/sqlalchemy/ext | |
parent | c4955c05a3ab40d53c83982da612e746c662640d (diff) | |
download | sqlalchemy-99ed3922678ac050888dad7ca1c2a2195d65e2c0.tar.gz |
- declarative_base() takes optional kwarg "mapper", which
is any callable/class/method that produces a mapper,
such as declarative_base(mapper=scopedsession.mapper).
This property can also be set on individual declarative
classes using the "__mapper_cls__" property.
Diffstat (limited to 'lib/sqlalchemy/ext')
-rw-r--r-- | lib/sqlalchemy/ext/declarative.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 62691a906..d8576d79b 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -162,6 +162,7 @@ from sqlalchemy.orm import synonym as _orm_synonym, mapper, comparable_property from sqlalchemy.orm.interfaces import MapperProperty from sqlalchemy.orm.properties import PropertyLoader, ColumnProperty from sqlalchemy import util, exceptions +import types __all__ = ['declarative_base', 'synonym_for', 'comparable_using', 'declared_synonym'] @@ -216,8 +217,12 @@ class DeclarativeMeta(type): inherits = cls.__mro__[1] inherits = cls._decl_class_registry.get(inherits.__name__, None) mapper_args['inherits'] = inherits - - cls.__mapper__ = mapper(cls, table, properties=our_stuff, **mapper_args) + + if hasattr(cls, '__mapper_cls__'): + mapper_cls = util.unbound_method_to_callable(cls.__mapper_cls__) + else: + mapper_cls = mapper + cls.__mapper__ = mapper_cls(cls, table, properties=our_stuff, **mapper_args) return type.__init__(cls, classname, bases, dict_) def __setattr__(cls, key, value): @@ -294,13 +299,15 @@ def comparable_using(comparator_factory): return comparable_property(comparator_factory, fn) return decorate -def declarative_base(engine=None, metadata=None): +def declarative_base(engine=None, metadata=None, mapper=None): lcl_metadata = metadata or MetaData() + if engine: + lcl_metadata.bind = engine class Base(object): __metaclass__ = DeclarativeMeta metadata = lcl_metadata - if engine: - metadata.bind = engine + if mapper: + __mapper_cls__ = mapper _decl_class_registry = {} def __init__(self, **kwargs): for k in kwargs: |