File: database.rst

package info (click to toggle)
flask-peewee 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,100 kB
  • sloc: javascript: 9,322; python: 4,053; makefile: 132; sh: 16
file content (122 lines) | stat: -rw-r--r-- 2,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
.. _database:

Database Wrapper
================

The Peewee database wrapper provides a thin layer of integration between flask
apps and the peewee orm.

The database wrapper is important because it ensures that a database connection
is created for every incoming request, and closed upon request completion.  It
also provides a subclass of ``Model`` which works with the database specified
in your app's configuration.

Most features of ``flask-peewee`` require a database wrapper, so you very likely
always create one.

The database wrapper reads its configuration from the Flask application.  The
configuration requires only two arguments, but any additional arguments will
be passed to the database driver when connecting:

`name`
    The name of the database to connect to (or filename if using sqlite3)

`engine`
    The database driver to use, must be a subclass of ``peewee.Database``.

.. code-block:: python

    from flask import Flask
    from peewee import *

    from flask_peewee.db import Database

    DATABASE = {
        'name': 'example.db',
        'engine': 'peewee.SqliteDatabase',
    }

    app = Flask(__name__)
    app.config.from_object(__name__)  # Load database configuration from module.

    # Instantiate the db wrapper.
    db = Database(app)

    # Start creating models.
    class Blog(db.Model):
        name = CharField()
        # .. etc.

You can also directly pass the Peewee database instance to the
:py:class:`Database` helper:

.. code-block:: python

    app = Flask(__name__)
    app.config.from_object(__name__)

    sqlite_db = SqliteDatabase('example.db')
    db = Database(app, sqlite_db)

    class Blob(db.Model):
        # ...

The database initialization can be deferred in order to support more dynamic
behavior:

.. code-block:: python

    from flask_peewee.db import Database

    # Defer initialization but define our models.
    db = Database()

    class Blog(db.Model):
        name = CharField()
        # .. etc.

    # Some time later, we can:
    app = Flask(__name__)
    app.config.from_object(__name__)
    db.init_app(app)

    # Or we can also specify the database directly.
    app = Flask(__name__)

    sqlite_db = SqliteDatabase('example.db')
    db.init_app(app, sqlite_db)


Other examples
--------------

To connect to MySQL using authentication:

.. code-block:: python

    DATABASE = {
        'name': 'my_database',
        'engine': 'peewee.MySQLDatabase',
        'user': 'db_user',
        'passwd': 'secret password',
    }

To connect to Postgresql using the playhouse ``PostgresqlExtDatabase``:

.. code-block:: python

    DATABASE = {
        'name': 'pg_database',
        'engine': 'playhouse.PostgresqlExtDatabase',
        'host': '127.0.0.1',
        'user': 'postgres',
        'port': 5432,
    }

We can specify the database directly, as well:

.. code-block:: python

    pg_db = PostgresqlDatabase('pg_database')

    db = Database(app, pg_db)