Python 将Flask SQLAlchemy中的模型分离到另一个文件会破坏PyDev导入检测
我正在学习Flask,当我尝试将我的应用程序分离到更多模块时,PyDev开始报告导入的Python 将Flask SQLAlchemy中的模型分离到另一个文件会破坏PyDev导入检测,python,flask,pydev,flask-sqlalchemy,Python,Flask,Pydev,Flask Sqlalchemy,我正在学习Flask,当我尝试将我的应用程序分离到更多模块时,PyDev开始报告导入的未定义变量。但是,代码仍然有效 首先,我首先向强制内置添加了flask,以便PyDev动态解析扩展。这样PyDev就不会报告flask.ext..imports 此设置一切正常: 模板/index.py <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <head> <titl
未定义变量。但是,代码仍然有效
首先,我首先向强制内置添加了flask
,以便PyDev动态解析扩展。这样PyDev就不会报告flask.ext..
imports
此设置一切正常:
模板/index.py
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<ul>
{% for user in all_users %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
</body>
</html>
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
from flask import Flask, render_template
from models import db, User
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
现在我将main\u old.py分为main.py和models.py,如下所示:
型号.py
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<ul>
{% for user in all_users %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
</body>
</html>
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
from flask import Flask, render_template
from models import db, User
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
main.py
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<ul>
{% for user in all_users %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
</body>
</html>
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username="", email=""):
self.username = username
self.email = email
from flask import Flask, render_template
from models import db, User
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db.init_app(app)
@app.route("/")
def hello():
db.drop_all()
db.create_all()
db.session.add(User("John Doe", "john.doe@example.com"))
db.session.add(User("Bill Smith", "smith.bill@example.com"))
db.session.commit()
all_users = User.query.all()
return render_template('index.html', all_users=all_users)
if __name__ == "__main__":
app.run(debug=True)
它是等效的,但我从导入中得到未定义的变量:query
在all\u users=User.query.all()
行。
为什么PyDev导入检测会像这样中断?为什么它在一个文件中工作?有办法解决这个问题吗?除了@UndefinedVariable
之外,还有其他变通方法吗?我仍然不知道为什么会发生这种情况,但变通方法是存在的
使用推荐的all\u users=db.session.query(User.all()
而不是all\u users=User.all()