coleifer / flask-peewee

flask integration for peewee, including admin, authentication, rest api and more
http://flask-peewee.readthedocs.org/
MIT License
776 stars 181 forks source link

Feature request: Delayed DB Engine initialization (add `init_app` method) #156

Open akaRem opened 9 years ago

akaRem commented 9 years ago

Hi!

I've recently read a book by Miguel Grinberg called Flasky
And found there a very widely used pattern of create_app function, see source

The idea is to create and register all extensions used in app and do not initialize them with concrete config/settings (because lots of things differ for testing/development/production).

The main parts of this pattern is:

a) definition of application

# file: app.py

from flask import Flask
from flask.ext.cool_thing import CoolThing
from config import config  # a set of different configs

cool_thing = CoolThing()  # just created, no app at this point, cool_thing not initialized

def create_app(config_name):
    # create an app
    app = Flask(__name__)
    # setup an app
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    # initialize all extensions
    cool_thing.init_app(app)
    return app

b) running an application with concrete settings

# file: manage.py

from app import create_app, cool_thing
from flask.ext.script import Manager

# create an app depending on envvars
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)

@manager.command
def do_stuff(a, b, c):
    cool_thing.do_some(a, b, c)

if __name__ == '__main__':
    manager.run()

At the moment interface of flask_peewee does not support delayed initialization

I don't have problems with such implementation. But I think that if flask_peewee could support this features, than it would be more handy to use by neophytes of Flask.

narzeja commented 9 years ago

Sounds like this pull request would solve it: https://github.com/coleifer/flask-peewee/pull/154 Similar issue: https://github.com/coleifer/flask-peewee/pull/121