0

我对 Python 有点陌生,我尝试建立一个类似 MVC 的项目。我用Quart白衣flask-SqlAlchemy

所以我的app.py样子是这样的:

from flask_sqlalchemy import SQLAlchemy
from config import SQLALCHEMY_STRING
from .setups import set_app

if __name__ == '__main__':
    app = set_app()
    app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_STRING
    db = SQLAlchemy(app)

    app.run(host='0.0.0.0')

现在我在名为的模型文件夹中创建了一个模型文件tests.py

from myapp.app import db
import datetime


class Test(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    text = db.Column(db.VARCHAR)
    created = db.Column(db.DATETIME, default=datetime.datetime.now())

    def __repr__(self):
        return '<Test %r>' % self.id, self.created


def insert_new(s):
    newt = Test(text=s)
    db.session.add(newt)
    db.session.commit()

我尝试在使用蓝图注册的路线中使用它app.py -> set_app()

import requests
from lxml import etree
from quart import jsonify
from quart.exceptions import HTTPException, HTTPStatus
from quart_openapi import PintBlueprint
from myapp.models import tests

tests= PintBlueprint('tests', __name__)


@tests.route('/tests/test', methods=['GET'])
async def push_credit():
    return tests.insert_new("Bla Bla")

我的代码在导入时中断from myapp.models import tests。在我尝试使用该模型之前,我在此文件中有其他可以正常工作的路线。

4

0 回答 0