From 41307cd7339a2a2aee0a3dd9c8b994df99d7eedb Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 14 Jan 2015 12:02:41 -0500 Subject: - add new section to ORM referring to runtime inspection API, more links, attempt to fix #3290 --- doc/build/orm/mapping_styles.rst | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/doc/build/orm/mapping_styles.rst b/doc/build/orm/mapping_styles.rst index e6be00ef7..7571ce650 100644 --- a/doc/build/orm/mapping_styles.rst +++ b/doc/build/orm/mapping_styles.rst @@ -119,3 +119,52 @@ systems ultimately create the same configuration, consisting of a :class:`.Table user-defined class, linked together with a :func:`.mapper`. When we talk about "the behavior of :func:`.mapper`", this includes when using the Declarative system as well - it's still used, just behind the scenes. + +Runtime Intropsection of Mappings, Objects +========================================== + +The :class:`.Mapper` object is available from any mapped class, regardless +of method, using the :ref:`core_inspection_toplevel` system. Using the +:func:`.inspect` function, one can acquire the :class:`.Mapper` from a +mapped class:: + + >>> from sqlalchemy import inspect + >>> insp = inspect(User) + +Detailed information is available including :attr:`.Mapper.columns`:: + + >>> insp.columns + + +This is a namespace that can be viewed in a list format or +via individual names:: + + >>> list(insp.columns) + [Column('id', Integer(), table=, primary_key=True, nullable=False), Column('name', String(length=50), table=), Column('fullname', String(length=50), table=), Column('password', String(length=12), table=)] + >>> insp.columns.name + Column('name', String(length=50), table=) + +Other namespaces include :attr:`.Mapper.all_orm_descriptors`, which includes all mapped +attributes as well as hybrids, association proxies:: + + >>> insp.all_orm_descriptors + + >>> insp.all_orm_descriptors.keys() + ['fullname', 'password', 'name', 'id'] + +As well as :attr:`.Mapper.column_attrs`:: + + >>> list(insp.column_attrs) + [, , , ] + >>> insp.column_attrs.name + + >>> insp.column_attrs.name.expression + Column('name', String(length=50), table=) + +.. seealso:: + + :ref:`core_inspection_toplevel` + + :class:`.Mapper` + + :class:`.InstanceState` -- cgit v1.2.1