我希望创建一个下拉菜单(在窗体中),用于按状态筛选客户。我正尝试创建一个子查询,以生成与请求的状态匹配的客户id
值。然后,我将在进一步筛选数据的主查询中使用此子查询。
下面代码中的错误是TypeError: 'BaseQuery' object is not callable
:status_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()
1条答案
按热度按时间t3irkdon1#
我了解到我只需要在查询语句的开头使用
db.session
: