diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-07-19 22:56:34 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-07-19 22:57:12 -0400 |
| commit | 37fc3e3bf8a481e00a54fdc83b0a927dffbba8a7 (patch) | |
| tree | a5ebc41fca348277c1ea97e30c05235f8e87c940 /lib/sqlalchemy/ext/declarative/api.py | |
| parent | a6069c75c2336421fd7b1c1397f87d33675eab27 (diff) | |
| download | sqlalchemy-37fc3e3bf8a481e00a54fdc83b0a927dffbba8a7.tar.gz | |
- Improved the examples in ``examples/generic_associations``, including
that ``discriminator_on_association.py`` makes use of single table
inheritance do the work with the "discriminator". Also
added a true "generic foreign key" example, which works similarly
to other popular frameworks in that it uses an open-ended integer
to point to any other table, foregoing traditional referential
integrity. While we don't recommend this pattern, information wants
to be free. Also in 0.8.3.
- Added a convenience class decorator :func:`.as_declarative`, is
a wrapper for :func:`.declarative_base` which allows an existing base
class to be applied using a nifty class-decorated approach. Also
in 0.8.3.
Diffstat (limited to 'lib/sqlalchemy/ext/declarative/api.py')
| -rw-r--r-- | lib/sqlalchemy/ext/declarative/api.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/sqlalchemy/ext/declarative/api.py b/lib/sqlalchemy/ext/declarative/api.py index 2f222f682..9cbe32267 100644 --- a/lib/sqlalchemy/ext/declarative/api.py +++ b/lib/sqlalchemy/ext/declarative/api.py @@ -218,6 +218,10 @@ def declarative_base(bind=None, metadata=None, mapper=None, cls=object, compatible callable to use as the meta type of the generated declarative base class. + .. seealso:: + + :func:`.as_declarative` + """ lcl_metadata = metadata or MetaData() if bind: @@ -237,6 +241,42 @@ def declarative_base(bind=None, metadata=None, mapper=None, cls=object, return metaclass(name, bases, class_dict) +def as_declarative(**kw): + """ + Class decorator for :func:`.declarative_base`. + + Provides a syntactical shortcut to the ``cls`` argument + sent to :func:`.declarative_base`, allowing the base class + to be converted in-place to a "declarative" base:: + + from sqlalchemy.ext.declarative import as_declarative + + @as_declarative() + 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 :func:`.as_declarative` are passed + along to :func:`.declarative_base`. + + .. versionadded:: 0.8.3 + + .. seealso:: + + :func:`.declarative_base` + + """ + def decorate(cls): + kw['cls'] = cls + kw['name'] = cls.__name__ + return declarative_base(**kw) + + return decorate class ConcreteBase(object): """A helper class for 'concrete' declarative mappings. |
