且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Flask-Admin 设置模型视图会导致导入错误

更新时间:2023-08-26 21:19:22

您的代码在 __init__.py 中有一行 from app.models import User.问题是 app.modelsfrom .导入数据库.这是一个循环导入:__init__ 尝试导入 User,它尝试导入 db,直到 __init__ 之后才定义 尝试导入 User.要解决此问题,请将您的本地应用导入移动到所有全局扩展内容的定义下方.

Your code has the line from app.models import User in __init__.py. The problem is that app.models has from . import db. This is a circular import: __init__ tries to import User, which tries to import db, which isn't defined until after __init__ tries to import User. To solve this, move your local app imports below the definitions of all the global extension stuff.

目前,您的代码类似于:

Currently, your code looks something like:

from flask_sqlalchemy import SQLAlchemy
from app.models import User

db = SQLAlchemy()

您需要将其更改为:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

from app.models import User