0
0
Flaskframework~5 mins

Querying with filter and filter_by in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the difference between filter() and filter_by() in Flask SQLAlchemy?

filter() accepts SQL expressions and allows complex queries using operators, while filter_by() uses keyword arguments for simple equality checks.

Click to reveal answer
beginner
How do you write a query to find all users with the name 'Alice' using filter_by()?

Use User.query.filter_by(name='Alice').all() to get all users named Alice.

Click to reveal answer
beginner
Write a query using filter() to find users older than 30.

Use User.query.filter(User.age > 30).all() to get users older than 30.

Click to reveal answer
intermediate
Can filter_by() handle complex conditions like age > 30?

No, filter_by() only supports simple equality checks. Use filter() for complex conditions.

Click to reveal answer
beginner
What does .all() do at the end of a query?

.all() runs the query and returns a list of all matching records.

Click to reveal answer
Which method allows you to write complex SQL expressions in Flask queries?
Afilter()
Bfilter_by()
Call()
Dget()
How would you find all users with the email 'test@example.com' using filter_by()?
AUser.query.filter(email='test@example.com').all()
BUser.query.get(email='test@example.com')
CUser.query.filter_by(email='test@example.com').all()
DUser.query.all(email='test@example.com')
What will User.query.filter(User.age > 25).all() return?
AUsers younger than 25
BUsers exactly 25 years old
CAll users
DUsers older than 25
Which method is NOT suitable for filtering by multiple conditions like name='Bob' and age > 20?
Afilter_by()
Bfilter(User.name=='Bob', User.age>20)
Cfilter().filter()
Dfilter()
What does the all() method do after a query?
ADeletes all matching records
BReturns all matching records as a list
CReturns the first matching record
DCounts all matching records
Explain how to use filter() and filter_by() in Flask SQLAlchemy queries. When would you choose one over the other?
Think about the complexity of the condition you want to apply.
You got /4 concepts.
    Describe how to find all users older than 30 using Flask SQLAlchemy. Include which method you use and why.
    Remember which method supports operators like >.
    You got /4 concepts.