2

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.

xGlorify
  • 119
  • 1
  • 12

1 Answers1

4

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()
Álvaro Justen
  • 1,943
  • 1
  • 17
  • 17
  • Thanks! This really helped me figure it out. I just am having a hard time comprehending how the filter_by() arguments is able to comprehend the kwarg and interpret it in the correct format even though it's a dict? Is that a feature of SQLAlchemy, being able to translate dictionary's to models and vice versa? – xGlorify Jan 30 '16 at 06:32
  • 1
    No, it's actually a Python feature for passing parameters to a function. Learn more at: http://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/ – Álvaro Justen Jan 30 '16 at 16:29