I still do not know why is this happening, but workaround exists.
Use recommended all_users = db.session.query(User).all()
instead of all_users = User.all()
.
Question
I am learning Flask and when I try to separate my app to more modules, for no apparent reason PyDev starts to report Undefined variable from import
. However, the code still works.
First, I have added flask
to Forced Builtins first so PyDev parses extensions dynamically. This way PyDev does not report flask.ext...
imports.
Everything is fine with this setup:
templates/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>
main_old.py
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)
Now I separate main_old.py into main.py and models.py like this:
models.py
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
main.py
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)
It is equivalent, but I get Undefined variable from import: query
on all_users = User.query.all()
line.
Why the PyDev import detection breaks like this? Why did it work in one file? Is there a way to fix this? Is there workaround other than @UndefinedVariable
?
Solution
I still do not know why is this happening, but workaround exists.
Use recommended all_users = db.session.query(User).all()
instead of all_users = User.all()
.