Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Is there a way to import all db.models for my shell? #147

Closed
rlam3 opened this issue Oct 27, 2015 · 7 comments
Closed

Is there a way to import all db.models for my shell? #147

rlam3 opened this issue Oct 27, 2015 · 7 comments

Comments

@rlam3
Copy link

rlam3 commented Oct 27, 2015

Is there a way to import all db.models for my shell when going interactive shell? I'm using sqlalchemy.

I'm using make_context and only able to pass models into the dictionary.... I want to be able to use the shell without having to do


from app.models.user import User 
etc...

every time I go into interactive shell

Thanks!

@davidism
Copy link

db.Model._decl_class_registry maps names to models. You can use the shell decorator to define your own context that includes these.

@jeffwidman
Copy link
Collaborator

FWIW, here's how I handle auto-imports of my models when I run python manage.py shell:

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)

def make_shell_context():
    from pprint import pprint
    from flask_sqlalchemy import get_debug_queries
    return dict(
        app=app, db=db,
        pprint=pprint, gq=get_debug_queries,
        Article=Article,
        ArticleCategory=ArticleCategory,
        ArticleComment=ArticleComment)

manager.add_command('shell', Shell(make_context=make_shell_context))

Works fine for me

@plaes
Copy link

plaes commented Dec 11, 2015

Jeff's example should end up in docs.

@rlam3
Copy link
Author

rlam3 commented Mar 16, 2016

@jeffwidman this would mean that if my models are in different folders, i would have to import them individually. I was hoping to import all models from a model folder and then each class within each module... more like a for loop

@jeffwidman
Copy link
Collaborator

I agree it's a pain. My wife was working on a somewhat similar problem recently around looping through imports and found the stdlib importlib useful. Afraid I haven't zero experience with it myself, so can't comment, but if you do explore and figure out a way to get it working, I'm definitely open to a PR putting an example in the docs.

@hectorcanto
Copy link

hectorcanto commented Nov 19, 2018

Just if somebody ends up here and don't know how to import shell

from flask_script import Shell

FWIW, here's how I handle auto-imports of my models when I run python manage.py shell:

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)

def make_shell_context():
    from pprint import pprint
    from flask_sqlalchemy import get_debug_queries
    return dict(
        app=app, db=db,
        pprint=pprint, gq=get_debug_queries,
        Article=Article,
        ArticleCategory=ArticleCategory,
        ArticleComment=ArticleComment)

manager.add_command('shell', Shell(make_context=make_shell_context))

Works fine for me

@FilippoLippi
Copy link

if someone would like to import all models from a package:

import inspect
from app import db, models

def make_shell_context():
    return dict(
        db=db,
        **dict(inspect.getmembers(models, inspect.isclass)))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants