Flask- SQLAlchemy-使用过滤器进行高级搜索

时间:2016-12-20 17:06:22

标签: flask html-select flask-sqlalchemy flask-wtforms

我正在使用一系列过滤器和html选择下拉菜单来尝试创建高级搜索。我希望用户能够选择他们想要搜索的各种字段并获得结果。我可以得到"搜索" (如果我只有4个字段有效用于查询'在下面的views.py中,但超过四个字段不返回任何结果,因为没有记录符合所有过滤器字段值。我知道会发生这种情况。

所以我一直试图弄清楚如何创建一个" - 全部 - "选项,以便用户可以选择他们最感兴趣的属性,以及他们想要的属性"查询"无视。

views.py(截断版本 - 还有几个要搜索的字段)

from flask import render_template, redirect, session, url_for, request, send_from_directory
from sqlalchemy import or_, and_, select
from flask.ext.security import login_required
from datetime import datetime
from RestorationPlantDB import app, db
from RestorationPlantDB import SPECIES, SPInfo, SPRef, Role, User

@app.route('/search_advanced', methods = ['GET', 'POST'])
def search_advanced():
    if request.method == 'POST':
        stratum = request.form.get('stratum')
        origin = request.form.get('origin')


        query = SPInfo.query.filter(and_(SPInfo.Stratum == stratum, 
                                         SPInfo.Origin == origin))

    return render_template('result_advancedsearch.html', year=datetime.now().year, query=query)

return render_template('search_advanced.html', year=datetime.now().year)

@app.route('/result_advancedsearch', methods = ['GET', 'POST'])
def result_advancedsearch():

    return render_template('result_advancedsearch.html', year=datetime.now().year)

search_advanced.html

{% extends "layout.html" %}

{% block content %}

<div class="container">

    <h1>Advanced Search</h1>

    <form method="POST" action="search_advanced">          
           <div class="advanced">
               <!--Select species stratum-->
               <label for="stratum"><p class="advanced">Lifeform</p></label>
               <select name="stratum" id="stratum">
                   <option value= ""> -- All -- </option>  <!-- Need to figure out how to pass an all option-->
                   <option value="Graminoid">Graminoid</option>
                   <option value="Forb">Forb</option>
                   <option value="Subshrub">Sub-Shrub</option>
                   <option value="Shrub">Shrub</option>
                   <option value="Tree">Tree</option>
                   <option value="Vine">Vine</option>
               </select>
           </div>

           <div class="advanced">
               <!--Select species origin-->
               <label for="origin"><p class="advanced">Origin</p></label>
               <select name="origin" id="origin">
                   <option value=""> -- All -- </option>  <!-- Need to figure out how to pass an all option-->
                   <option value="Native">Native</option>
                   <option value="Exotic">Exotic</option>
               </select>
            </div>

 </form>
</div>
{% endblock %}

1 个答案:

答案 0 :(得分:1)

使用SQLAlchemy,您可以根据可用参数有条件地构建查询。基本上,您可以根据需要多次应用filter()filter_by()或任何其他查询方法,SQLAlchemy将了解如何构建查询。

query = SPInfo.query
if stratum:
    query = query.filter(SPInfo.Stratum == stratum)
if origin:
    query = query.filter(SPInfo.Origin == origin)
if elev:
    query = query.filter(SPInfo.Elevation.contains(elev))

results = query.all()

对于更干净的代码,您需要构建一个元素列表来测试如何应用这些过滤器,然后使用循环来执行上述操作。