I have two tables which I am joining together.
class Request(db.Model):
id = db.Column(db.Integer, primary_key=True)
client = db.Column(db.Integer, db.ForeignKey('client.id'))
offerSent = db.Column(db.Boolean)
(...)
class Client(db.Model):
id = db.Column(db.Integer, primary_key=True)
lastname = db.Column(db.String(60))
prename = db.Column(db.String(40))
requests = db.relationship('Request', backref='requestClient', lazy='dynamic')
(...)
So I am joining the two tables:
@app.route('/')
@app.route('/index')
@login_required
def index():
results = db.session.query(Request, Client).filter(Request.client == Client.id).all()
return render_template('index.html', title='Start', results=results)
And then I am displaying them:
{% block app_content %}
<h4>User: {{ current_user.username }}!</h4>
{% for i in range(0, results|length) %}
{{ results[i][0].offerSent }}
{{ results[i][1].lastname }}
{% endfor %}
{% endblock %}
So as you see I have to hardcode the second dimension to get the proper value out of the list (results = [( Request 1, Client 13), (...,...)].
Is there maybe a nicer way to do it? E.g. something like results.request.offerSent and results.client.lastname?