Skip to content

store app instance in class #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2016
Merged

Conversation

TennyZhuang
Copy link
Contributor

@TennyZhuang TennyZhuang commented Sep 19, 2016

access app instance from engine.

I use factory pattern config in my project, such as

import os

basedir = os.path.abspath(os.path.dirname(__file__))


class BaseConfig(object):
    pass

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(BaseConfig):
    DEBUG = True


class ProductionConfig(BaseConfig):
    pass


config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig,
}

so I have to use app.config to get my config.

if I want the config in models file, I need an app instance

and from flask import current_app will cause an error RuntimeError: Working outside of application context.

The reasonable way to solve this is store app in MongoEngine class, such as what flask-script, line 80, do

so I can use

db = MongoEngine()
db.init_app(app)
app = db.app

@wojcikstefan
Copy link
Member

This makes sense to me. Many Flask-Whatever extensions assign app passed to init_app to self.app.

@wojcikstefan
Copy link
Member

@TennyZhuang are you still interested in this PR? If so, can you rebase against the latest master? I'd love to make it happen!

@wojcikstefan
Copy link
Member

Note to self and others - the init_app approach is also encouraged by official Flask docs (see e.g. http://flask.pocoo.org/docs/0.12/extensiondev/#the-extension-code).

@TennyZhuang
Copy link
Contributor Author

@wojcikstefan rebased

@wojcikstefan
Copy link
Member

Thanks @TennyZhuang !

@wojcikstefan wojcikstefan merged commit e89cec0 into MongoEngine:master Dec 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants