python - How to formulate SQLAlchemy queries to load columns and tuples multiple models in an .html file? -
i making "my pages"-page online application users can view information previous , upcoming orders. goal display placed orders, contents, associated given user id. want divide orders whether have been collected or not in order separate order history orders have not yet been collected.
in order handle information orders , products i've made database in file named models.py containting following classes:
class user(db.model): id = db.column(db.integer, primary_key=true) email = db.column(db.unicode(120), index=true, unique=true) password = db.column(db.unicode(20)) orders = db.relationship('order', backref='user', lazy='dynamic') def __repr__(self): return '<user %r>' % self.id class product(db.model): id = db.column(db.integer, primary_key=true) name = db.column(db.unicode(120), index=true, unique=true, nullable=false) price = db.column(db.integer, index=true, nullable=false) orders = db.relationship('order_contents', backref='product', lazy='dynamic') def __repr__(self): return '<product %r>' % (self.name) class order(db.model): ordid = db.column(db.integer, primary_key=true) owner = db.column(db.integer, db.foreignkey(user.id), index=true, unique=true, nullable=false) cancelled = db.column(db.boolean, default=false, index=true, nullable=false) collected = db.column(db.boolean, default=false, index=true, nullable=false) contents = db.relationship('order_contents', backref='order', lazy='dynamic') def __repr__(self): return '<order %r>' % (self.ordid) class order_contents(db.model): orderid = db.column(db.integer, db.foreignkey('order.ordid'), primary_key=true) productid = db.column(db.integer, db.foreignkey('product.id'), primary_key=true) pickupdate = db.column(db.date, primary_key=true) def __repr__(self): return '<order: %r, product: %r>' % (self.orderid, self.productid)
i load relevant information mypages.html (and have far tried using queries) unable correctly. i've tried following in file named views.py (here trying user.id = 1):
from flask import render_template app import app, db, models .models import user, product, order, order_contents @app.route('/mypages') def mypages(): upcoming_orders = models.product.query.join(order_contents, product.id == order_contents.productid).\ join(order, order_contents.orderid == order.ordid).\ filter(and_(order.owner == '1', order.cancelled == false, order.collected == false)).\ order_by(order_contents.pickupdate).all() previous_orders = models.order.query.join(order_contents, order.ordid == order_contents.orderid).\ join(product, order_contents.productid == product.id).\ filter(order.owner == '1').\ order_by(order_contents.pickupdate).all() return render_template('mypages.html', coming=upcoming_orders, history=previous_orders)
and hough not error unable access information in mypages.html. in html.file trying write out information using following code (here order.ordid):
{% order in coming %} <p href="#">orderid: {{order.ordid}} </p> {% endfor %}
how can formulate queries in order access information need in mypages.html? working in pycharm python, flask , sqlalchemy.
Comments
Post a Comment