google 了 3 天反复测试也找不到答案的 Flask-SQLAlchemy 问题,请大神于水火间拯救我

2020-11-23 21:53:22 +08:00
 sunnylemon
求教各位大佬:

订单表( FuckOrders )的 diy_order_phoneShellID 字段,对应 商品型号表( PhonesShell )的 phoneShellID 字段。
商品型号表有一个 phoneShellDesc 商品型号描述字段。

对应的 sql 语句是
select * from diy_orders o
INNER JOIN diy_phone_shells s
on o.diy_order_phoneShellID=s.phoneShellID
where diy_order_status=2
ORDER BY o.createDateObj DESC

我想把订单表中每个订单所对应的 phoneShellDesc 字段显示到网页中。
请问上面的语句应该怎么样改呢? 谢谢指点

py 文件代码:

from flask import Flask, request, flash, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1:3306/diy'
app.config['SECRET_KEY'] = "random string"

db = SQLAlchemy(app)


class FuckOrders(db.Model):
__tablename__ = 'diy_orders'
#id = db.Column('diy_order_id', db.Integer, primary_key = True)
diy_order_id = db.Column(db.Integer, primary_key = True)
diy_order_receiver = db.Column()
diy_order_phone = db.Column()
diy_order_province= db.Column()
diy_order_city = db.Column()
diy_order_area = db.Column()
diy_order_detail= db.Column()
diy_order_status= db.Column()
diy_order_count = db.Column()
diy_order_phoneShellID=db.Column()
diy_order_orderDealMoneyFen=db.Column()
diy_order_renderImgUrl= db.Column()
diy_order_renderImgWithShellUrl= db.Column()
createDateObj= db.Column()
diy_order_ship= db.Column()
#phoneShellDesc 这个是否需要声明?



def __init__(self, diy_order_receiver, diy_order_phone,diy_order_city, diy_order_area,diy_order_count,diy_order_detail,diy_order_status,diy_order_orderDealMoneyFen,diy_order_phoneShellID,diy_order_renderImgUrl,diy_order_renderImgWithShellUrl,createDateObj,diy_order_ship,diy_order_phoneShellDESC):
self.diy_order_receiver = diy_order_receiver
self.diy_order_phone = diy_order_phone
self.diy_order_city = diy_order_city
self.diy_order_area = diy_order_area
self.diy_order_count = diy_order_count
self.diy_order_detail= diy_order_detail
self.diy_order_status=diy_order_status
self.diy_order_phoneShellID=diy_order_phoneShellID
self.diy_order_orderDealMoneyFen=diy_order_orderDealMoneyFen
self.diy_order_renderImgUrl=diy_order_renderImgUrl
self.diy_order_renderImgWithShellUrl=diy_order_renderImgWithShellUrl
self.createDateObj=createDateObj
self.diy_order_ship=diy_order_ship
self.diy_order_phoneShellDESC=diy_order_phoneShellDESC


class PhonesShell(db.Model):
__tablename__ = 'diy_phone_shells'
id = db.Column('phoneShellKeyId', db.Integer, primary_key = True)
phoneShellID = db.Column(db.Integer)
phoneShellDesc = db.Column(db.String)


def __init__(self, phoneShellID,phoneShellDesc):
self.phoneShellID = phoneShellID
self.phoneShellDesc = phoneShellDesc


@app.route('/')
def Show_Orders()
return render_template('Order_Lists.html',HiOrders=FuckOrders.query.outerjoin(PhonesShell, PhonesShell.phoneShellID==FuckOrders.diy_order_phoneShellID).all())


if __name__ == '__main__':
app.run(debug = True)
1661 次点击
所在节点    Python
7 条回复
no1xsyzy
2020-11-24 01:35:07 +08:00
xiaoqiao24
2020-11-24 09:22:58 +08:00
FuckOrders.query.filter(FuckOrders.diy_order_status==2).join(PhonesShell, PhonesShell.phoneShellID==FuckOrders.diy_order_phoneShellID).order_by(FuckOrders.createDateObj.desc())

试试看对不对
sunnylemon
2020-11-24 11:01:56 +08:00
@xiaoqiao24 谢谢大佬。可以正常运行,但是要怎么样才能把这个字段 [ PhonesShell.phoneShellDesc ] 绑定到网页里边呢?
l4ever
2020-11-24 12:43:45 +08:00
l4ever
2020-11-24 12:45:40 +08:00
SjwNo1
2020-11-24 16:15:15 +08:00
`db.session.query(...).join(table, conds).order_by`
xiaoqiao24
2020-11-25 08:58:36 +08:00
@sunnylemon 什么叫做绑定到网页里面,这个属于你的业务逻辑范畴了吧?!既不贴代码,又不说明使用场景,你让我等怎么猜?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/728510

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX