[Python] Accessing User model from Flask shell raises NameError

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    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 try to access the User model, I get NameError: name 'User' is not defined. How can I access my models from the Flask shell?

    def create_app(config_name):
    application = Flask(__name__)
    application.config.from_object(config[config_name])
    db.init_app(application)

    from user import user
    application.register_blueprint(user, url_prefix='/user')

    return application

    def create_cli_app(info):
    return create_app('develop')

    @click.group(cls=FlaskGroup, create_app=create_cli_app)
    def cli():
    pass

    if __name__ == '__main__':
    cli()

    Continue reading...

Compartilhe esta Página