Reflection
The reflection module contains helpers for introspecting existing databases. This module is used internally by several other modules in the playhouse, including DataSet and pwiz, a model generator.
class Introspector
(metadata[, schema=None])
Metadata can be extracted from a database by instantiating an Introspector
. Rather than instantiating this class directly, it is recommended to use the factory method from_database()
.
classmethod
from_database
(database[, schema=None])Parameters: - database – a
Database
instance. - schema (str) – an optional schema (supported by some databases).
Creates an
Introspector
instance suitable for use with the given database.Usage:
db = SqliteDatabase('my_app.db')
introspector = Introspector.from_database(db)
models = introspector.generate_models()
# User and Tweet (assumed to exist in the database) are
# peewee Model classes generated from the database schema.
User = models['user']
Tweet = models['tweet']
- database – a
generate_models
([skip_invalid=False[, table_names=None[, literal_column_names=False[, bare_fields=False[, include_views=False]]]]])Parameters: - skip_invalid (bool) – Skip tables whose names are invalid python identifiers.
- table_names (list) – List of table names to generate. If unspecified, models are generated for all tables.
- literal_column_names (bool) – Use column-names as-is. By default, column names are “python-ized”, i.e. mixed-case becomes lower-case.
- bare_fields – SQLite-only. Do not specify data-types for introspected columns.
- include_views – generate models for VIEWs as well.
Returns: A dictionary mapping table-names to model classes.
Introspect the database, reading in the tables, columns, and foreign key constraints, then generate a dictionary mapping each database table to a dynamically-generated
Model
class.