You can use the request arguments together with the **kwargs functionality python has. Lets pretend we want to look for someone in our userdatabase named bob, which is 22 years old:
http://127.0.0.1:5000/?name=bob&age=22
You can convert the request arguments to a dictionary like this:
argumentdict = request.args.to_dict()
You can then query our users using the kwargs
users = User.query.filter_by(**argumentdict).all()
which is the same thing as:
users = User.query.filter_by(name='bob', age='22').all()
which will then put all users with the name bob and the age 22 in a list.
Fully working snippet:
from flask import Flask, render_template, request, render_template_string
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
age = db.Column(db.Integer)
db.create_all()
for name, age in [['Bob', 45], ['John', 55], ['Bob', 22]]:
user = User()
user.name = name
user.age = age
db.session.add(user)
db.session.commit()
@app.route("/")
def index():
argumentdict = request.args.to_dict()
users = User.query.filter_by(**argumentdict).all()
print(users)
app.run()
with this query: http://127.0.0.1:5000/?name=bob&age=22, will print:
[<User 3>]
It will give you errors though, when using it with invalid request arguments (name=bob&age=22&country=russia), so you have to take that into account.
EDIT: I completely missed the PONY ORM bit, sorry about that. I don't know anything about it, but if you can query in the same way as in SQL-Alchemy (column1=value1, column2=value2, ...) it should work with this example as well. I'll leave it here anyways because it's still useful for those who use SQL-Alchemy.