Answer the question
In order to leave comments, you need to log in
How to correctly compose a join request in a Flask application?
Models:
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
username = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120))
def __init__(self, name, username, password):
self.name = name
self.username = username
self.password = password
class Weather(db.Model):
__tablename__ = 'weather'
id = db.Column(db.Integer, primary_key=True)
ray_id = db.Column(db.Integer, db.ForeignKey('users.id'))
ray = db.relationship('User', backref=db.backref('weather_current', uselist=False))
putdate = db.Column(db.DateTime)
sost = db.Column(db.String(250))
temp = db.Column(db.String(150))
def __init__(self, ray_id, putdate, sost, temp):
self.ray_id = ray_id
self.putdate = putdate
self.sost = sost
self.temp = temp
@mod.route('/', methods = ['GET', 'POST'])
def weather():
rows = Weather.query.outerjoin(User, Weather.ray_id==User.id).order_by(User.name.asc())
return render_template("weather/weather.html",
rows = rows)
{% for row in rows %}
<tr>
<td align=left>{{ row.name }}</td>
<td>{{ row.putdate }}</td>
<td>{{ row.sost }}</td>
<td>{{ row.temp }}</td>
</tr>
{% endfor %}
{{ row.username }}
SELECT * FROM weather LEFT JOIN users ON weather.id_ray = users.id ORDER BY users.name asc"
Answer the question
In order to leave comments, you need to log in
{{ row.ray.username }}
And instead
You can just write
Full example:
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tst.db'
db = SQLAlchemy(app)
import datetime
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
username = db.Column(db.String(120), unique=True)
password = db.Column(db.String(120))
def __init__(self, name, username, password):
self.name = name
self.username = username
self.password = password
class Weather(db.Model):
__tablename__ = 'weather'
id = db.Column(db.Integer, primary_key=True)
ray_id = db.Column(db.Integer, db.ForeignKey('users.id'))
ray = db.relationship('User', backref=db.backref('weather_current', uselist=False))
putdate = db.Column(db.DateTime)
sost = db.Column(db.String(250))
temp = db.Column(db.String(150))
def __init__(self, ray_id, putdate, sost, temp):
self.ray_id = ray_id
self.putdate = putdate
self.sost = sost
self.temp = temp
db.drop_all()
db.create_all()
u = User("usertest2", 'testuser2', '123')
db.session.add(u)
db.session.commit()
db.session.add(Weather(u.id, datetime.datetime.now(), 'test', '123'))
db.session.add(Weather(None, datetime.datetime.now(), 'test', '123'))
db.session.commit()
@app.route('/', methods = ['GET', 'POST'])
def weather():
rows = Weather.query.outerjoin(User).order_by(User.name.asc())
return render_template("weather.html",
rows = rows)
if __name__ == "__main__":
app.run(debug=True)
<table>
{% for row in rows %}
<tr>
<td align=left>{{ row.ray.name }}</td>
<td>{{ row.putdate }}</td>
<td>{{ row.sost }}</td>
<td>{{ row.temp }}</td>
<td>{{ row.ray.username }}</td>
</tr>
{% endfor %}
</table>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question