Skip to content
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

RuntimeError: working outside of application context #8

Closed
rmasters opened this issue Dec 10, 2014 · 5 comments

Comments

@rmasters
Copy link

commented Dec 10, 2014

I'm using Flask in the structure defined here and am coming across this error:

Traceback (most recent call last):
  File "run.py", line 2, in <module>
    from app import app
  File ".../app/__init__.py", line 13, in <module>
    from monitors.controllers import monitors
  File ".../app/monitors/controllers.py", line 3, in <module>
    from .models import Monitor, monitor_schema
  File ".../app/monitors/models.py", line 16, in <module>
    monitor_schema = MonitorSchema()
  File ".../site-packages/marshmallow/schema.py", line 227, in __init__
    self.opts = self.OPTIONS_CLASS(self.Meta)
  File ".../site-packages/flask_marshmallow/__init__.py", line 56, in __init__
    meta, 'strict', current_app.config['MARSHMALLOW_STRICT']
  File ".../site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File ".../site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File ".../site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

I'm passing through the app instance as below (which should retain the app context?):

init.py:
app = Flask(__name__)
ma = Marshmallow(app)
monitors/models.py
from app import app, ma

class MonitorSchema(ma.Schema):
    class Meta:
        fields = ('id', 'name')

monitor_schema = MonitorSchema()
monitors/controllers.py
from .models import Monitor, monitor_schema

...
monitor = Monitor.query.get(1)
return monitor_schema.dump(monitor).data

Any ideas as to where I'm going wrong? Thanks

@rmasters

This comment has been minimized.

Copy link
Author

commented Dec 10, 2014

It seems a way to work around it is to init the schema object under the app_context, but I'd like to know if I'm doing something ill-advised :)

monitors/models.py
from app import app, db, ma

class Model(db.Model):
    pass

class ModelSchema(ma.Schema):
    pass

with app.app_context():
    model_schema = ModelSchema()
    models_schema = ModelSchema(many=True)
@sloria

This comment has been minimized.

Copy link
Member

commented Dec 22, 2014

@rmasters Indeed, the app context is necessary in order for flask-marshmallow to pull the app config values (MARSHMALLOW_STRICT and MARSHALLOW_DATEFORMAT).

We may deprecate these config options in a future release. Now that you've brought it up, I'm not sure that they add much value, since you can just create base schema class with the desired configuration.

sloria added a commit that referenced this issue Dec 22, 2014

@mr337

This comment has been minimized.

Copy link

commented Jan 11, 2015

+1 for this as this just bit me too

@sloria sloria closed this in 46968f5 Jan 18, 2015

@melvinmt

This comment has been minimized.

Copy link

commented Mar 29, 2015

Thanks for fixing this, it works in the dev branch. Please consider upgrading the default installation when installing it through pip install flask-marshmallow as well.

@sloria

This comment has been minimized.

Copy link
Member

commented Mar 30, 2015

This is now released to the PyPI (version 0.5.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.