html SQLAlchemy子查询- 'BaseQuery'对象不可调用

tvokkenx  于 2022-12-09  发布在  其他
关注(0)|答案(1)|浏览(131)

我希望创建一个下拉菜单(在窗体中),用于按状态筛选客户。我正尝试创建一个子查询,以生成与请求的状态匹配的客户id值。然后,我将在进一步筛选数据的主查询中使用此子查询。
下面代码中的错误是TypeError: 'BaseQuery' object is not callablestatus_query = appts_db.query(appts_db.id).subquery() .

索引.html

<form action="/" method="GET">
    <select name="status">
        <option value = "All" 
            {% if status_selection == "All" %} selected {% endif %}>All</option>

        <option value = "Scheduled"
            {% if status_selection == "Scheduled" %} selected {% endif %}>Scheduled</option>

        <option value = "Completed" 
            {% if status_selection == "Completed" %} selected {% endif %}>Completed</option>
    </select>
</form>

模型.py

class appts_db(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    customer = db.Column(db.String(100))
    status = db.Column(db.String(30))
    pickup_date = db.Column(db.String(10))

查看次数.py

@views.route('/')
def index():
    status_selection = request.args.get('status')

    # Subquery:
    if status_selection == 'All':
        status_query = appts_db.query(appts_db.id).subquery()
    elif status_selection == 'Scheduled':
        status_query = appts_db.query.filter(appts_db.status == 'Scheduled').subquery()
    elif status_selection == 'Completed':
        status_query = appts_db.query.filter(appts_db.status == 'Completed').subquery()

    # Main query:
    appts = appts_db.query.join(status_query, appts_db.id == status_query.id) \
        .order_by(appts_db.pickup_date).all()
t3irkdon

t3irkdon1#

我了解到我只需要在查询语句的开头使用db.session

from flask import session

@views.route('/')
def index():
    status_selection = request.args.get('status')
    
    # Subquery:
    if status_selection == 'All':
        status_query = db.session.query(appts_db.id).subquery()
    elif status_selection == 'Scheduled':
        status_query = db.session.query(appts_db.id) \
            .filter(appts_db.status == 'Scheduled').subquery()
    elif status_selection == 'Completed':
        status_query = db.session.query(appts_db.id) \
            .filter(appts_db.status == 'Completed').subquery()
    else:
        status_query = db.session.query(appts_db.id).subquery()

    #Main query:
    appts = db.session.query(appts_db) \
        .filter(appts_db.id.in_(status_query)) \
        .order_by(appts_db.pickup_date).all()

相关问题