V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sunnylemon
V2EX  ›  Python

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

  •  
  •   sunnylemon · 2020-11-23 21:53:22 +08:00 · 1649 次点击
    这是一个创建于 1442 天前的主题,其中的信息可能已经有所发展或是发生改变。
    求教各位大佬:

    订单表( 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:[email protected]: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)
    7 条回复    2020-11-25 08:58:36 +08:00
    no1xsyzy
        1
    no1xsyzy  
       2020-11-24 01:35:07 +08:00
    xiaoqiao24
        2
    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
        3
    sunnylemon  
    OP
       2020-11-24 11:01:56 +08:00
    @xiaoqiao24 谢谢大佬。可以正常运行,但是要怎么样才能把这个字段 [ PhonesShell.phoneShellDesc ] 绑定到网页里边呢?
    l4ever
        4
    l4ever  
       2020-11-24 12:43:45 +08:00
    l4ever
        5
    l4ever  
       2020-11-24 12:45:40 +08:00
    SjwNo1
        6
    SjwNo1  
       2020-11-24 16:15:15 +08:00
    `db.session.query(...).join(table, conds).order_by`
    xiaoqiao24
        7
    xiaoqiao24  
       2020-11-25 08:58:36 +08:00
    @sunnylemon 什么叫做绑定到网页里面,这个属于你的业务逻辑范畴了吧?!既不贴代码,又不说明使用场景,你让我等怎么猜?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5543 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 08:32 · PVG 16:32 · LAX 00:32 · JFK 03:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.