pythonpostgresqlflask-sqlalchemydynamicquerydynamic-queries

Remove objects from query if None or Null


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.


Solution

  • 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()