0

I'm trying to run a SQLAlchemy query in my Flask app using. I want to grab the URL parameter ('menu') and substitute it's value into my query...

@app.route('/attend/')
def attend_by_role():
    role = request.args.get("menu")
    attendee_by_role = session.query(Attendee).filter_by(role = True)
    return render_template("attending.html", attendee = attendee_by_role) 

This is directly looking for role in my table, which I don't want. I want to look up what the role variable is as defined by the URL parameter.

3
  • For example if role == 'admin' you want only the entries that have 'admin' as a role? Commented Aug 4, 2014 at 21:16
  • My table has several columns (i.e. frontend, backend, design, admin), all of which have Boolean entry values. I want to run a query so that role represents the column name and returns only the rows where the entry values are True (in that column). This is probably simple but I'm new to SQL Alchemy! Commented Aug 4, 2014 at 21:26
  • 2
    Do you mean you want to filter by the column specified by the menu parameter? Try .filter_by(**{role: True}). Commented Aug 4, 2014 at 21:32

1 Answer 1

2

You need to use a dictionary to get the value of role.

attendee_by_role = session.query(Attendee).filter_by(**{role: True})

Alternatively you can use filter instead.

attendee_by_role = session.query(Attendee).filter(getattr(Attendee, role) == True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.