Accessing User Model From Flask Shell Raises Nameerror
I want to add a user to the database from the Flask shell started by the shell command. I'm using an app factory, so I create my own FlaskGroup cli: python myapp.py shell. When I t
Solution 1:
All shell
does is launch a shell with your app loaded and an app context pushed. Other than that, it's exactly like any other Python shell by default. You still have to import things if you want to use them, hence the name error.
Use the app.shell_context_processor
decorator to inject other things into the shell. Each decorated function returns a dict of names to inject.
defcreate_app():
...
from myapp.users.models import User
@app.shell_context_processordefinject_models():
return {
'User': User,
}
...
Post a Comment for "Accessing User Model From Flask Shell Raises Nameerror"