If you install peewee using setup.py install, pwiz will be installed as a “script” and you can just run:

    This will print a bunch of models to standard output. So you can do this:

    1. python -m pwiz -e postgresql my_postgres_db > mymodels.py
    2. python # <-- fire up an interactive shell

    pwiz accepts the following command-line options:

    • sqlite
    • mysql
    • postgresql

    Warning

    If a password is required to access your database, you will be prompted to enter it using a secure prompt.

    The password will be included in the output. Specifically, at the top of the file a will be defined along with any required parameters – including the password.

    pwiz examples

    1. # Introspect a Sqlite database.
    2. python -m pwiz -e sqlite path/to/sqlite_database.db
    3. # Introspect a MySQL database, logging in as root. You will be prompted
    4. # for a password ("-P").
    5. python -m pwiz -e mysql -u root -P mysql_db_name
    6. python -m pwiz -e postgres -u postgres -H 10.1.0.3 pg_db_name

    Full example:

    Produces the following output:

    1. from peewee import *
    2. database = SqliteDatabase('example.db', **{})
    3. class UnknownField(object):
    4. def __init__(self, *_, **__): pass
    5. class BaseModel(Model):
    6. class Meta:
    7. database = database
    8. class User(BaseModel):
    9. class Meta:
    10. table_name = 'user'
    11. class Tweet(BaseModel):
    12. content = TextField()
    13. timestamp = DateTimeField()
    14. user = ForeignKeyField(column_name='user_id', field='id', model=User)
    15. class Meta:
    16. table_name = 'tweet'

    Observations:

    • The foreign-key Tweet.user_id is detected and mapped correctly.
    • The User.username UNIQUE constraint is detected.
    • Each model explicitly declares its table name, even in cases where it is not necessary (as Peewee would automatically translate the class name into the appropriate table name).
    • All the parameters of the are explicitly declared, even though they follow the conventions Peewee uses by default.

    The UnknownField is a placeholder that is used in the event your schema contains a column declaration that Peewee doesn’t know how to map to a field class.