SQLAchemy'未找到应用程序.在视图函数内部工作或“推" [英] SQLAchemy 'No application found. Either work inside a view function or push'

查看:82
本文介绍了SQLAchemy'未找到应用程序.在视图函数内部工作或“推"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ello ello,

Ello ello,

我在遇到的错误中发现了类似的问题,并尝试了提供的解决方案,但对我而言不起作用.

I found similar questions on the bug i'm facing, and tried the solutions offered but it didn't work for me.

我正在尝试将模型分离到另一个目录中,并将其导入到app.py

I'm trying to separate out my models in a different directory and import them into the app.py

当我尝试将db导入python终端时,找不到任何应用程序.

When I try to import the db into the python terminal, i'm getting the no application found.

app.py代码

from flask import Flask
from flask_restful import Resource, Api
# from flask_sqlalchemy import SQLAlchemy
from routes import test, root, user
from models.todo import db

app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:pass123@localhost/db'
app.config['SECRET_KEY'] = 'thiskeyissecret'
# db.init_app(app)

with app.app_context():
    api = Api(app)
    db.init_app(app)




api.add_resource(root.HelloWorld, '/')
api.add_resource(test.Test, '/test')
api.add_resource(user.User, '/user')

if __name__ == '__main__':
    app.run(debug=True)

模型

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Todo(db.Model):
    __tablename__ = 'Todos'
    id = db.Column('id', db.Integer, primary_key=True)
    data = db.Column('data', db.Unicode)

    def __init__(self, id, data):
        self.id = id
        self.data = data

    def __repr__(self):
        return '<Todo %>' % self.id

我的文件目录如下

Main_app

  • 型号
    • Todo.py
    • Models
      • Todo.py
      • 一些路线

      推荐答案

      Flask-SQLAlchemy需要活动的应用程序上下文.

      Flask-SQLAlchemy needs an active application context.

      尝试:

      with app.app_context():
          print(Todo.query.count())
      

      来自烧瓶文档:

      背景的目的

      Purpose of the Context

      Flask应用程序对象具有以下属性,例如config 在视图和CLI命令中访问很有用.但是,导入 您项目中模块内的应用实例容易出现循环通知 导入问题.使用应用程序工厂模式或编写可重用时 蓝图或扩展程序,将没有要导入的应用程序实例 全部.

      The Flask application object has attributes, such as config, that are useful to access within views and CLI commands. However, importing the app instance within the modules in your project is prone to circular import issues. When using the app factory pattern or writing reusable blueprints or extensions there won’t be an app instance to import at all.

      Flask通过应用程序上下文解决了此问题.而不是 直接引用应用程序,您可以使用current_app代理, 指向处理当前活动的应用程序.

      Flask solves this issue with the application context. Rather than referring to an app directly, you use the the current_app proxy, which points to the application handling the current activity.

      Flask在处理以下内容时会自动推送应用程序上下文 要求.查看函数,错误处理程序和其他正在运行的函数 在请求期间将可以访问current_app.

      Flask automatically pushes an application context when handling a request. View functions, error handlers, and other functions that run during a request will have access to current_app.

      这篇关于SQLAchemy'未找到应用程序.在视图函数内部工作或“推"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆