Remove objects from query if None or Null

谁说胖子不能爱 提交于 2021-01-28 00:48:46

问题


I am trying to build a query that takes form data and removes any None or " " submissions but I'm not sure how to approach the logic. Here is the code;

@app.route('/filterassets', methods=['GET', 'POST'])
def searchassets():
form = FilterAssetsForm()
results = None
if request.method == "POST":
    if form.validate_on_submit():
        try:
            horsepower = form.horsepower_search.data
            voltage = form.voltage_search.data
            rpm = form.rpm_search.data
            results = Motor.query.filter_by(horsepower=horsepower, voltage=voltage, rpm=rpm).all()
        except Exception as e:  
            flash(e)
            db.session.rollback()
        return render_template('filterassets.html', form=form, results=results)
return render_template('filterassets.html', form=form)

Because it's the backend of a page that lets users filter, rather than explicitly search, some form data is empty. This is causing the query to search for results where one or more forms == None.

Example: User enters 100 in the horsepower form and leaves RPM and Voltage blank. The query returns no results even though there are rows with 100 horsepower because it is looking for rows where Horsepower == 100 and RPM and Voltage == None.

I've tried using filter(None, object) with no success, and think the solution may be somewhere in using kwargs.

I know I need to pass all the form data to something that will remove None or Null entries, then pass it onto the final Motor.query.filter_by argument, I just don't know how.


回答1:


You can create a dict with the filter data:

filter_data = {'horsepower': horsepower, 'voltage': voltage, 'rpm': rpm}

Then create another dict with only the keys which values exist:

filter_data = {key: value for (key, value) in filter_data.items()
               if value}

And finally use this dict as kwargs to your query:

results = Motor.query.filter_by(**filter_data).all()


来源:https://stackoverflow.com/questions/35097921/remove-objects-from-query-if-none-or-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!