Bird
0
0

You want to implement a repository method that returns all users whose email ends with '@example.com'. Which of these is the best way to write it using SQLAlchemy in Flask?

hard📝 Application Q15 of 15
Flask - Ecosystem and Patterns
You want to implement a repository method that returns all users whose email ends with '@example.com'. Which of these is the best way to write it using SQLAlchemy in Flask?
Adef get_example_users(self): return User.query.filter(User.email.contains('@example.com')).first()
Bdef get_example_users(self): return User.query.filter_by(email='@example.com').all()
Cdef get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all()
Ddef get_example_users(self): return User.query.filter(User.email == '@example.com').all()
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering by string suffix in SQLAlchemy

    To filter emails ending with a string, use filter(User.email.endswith(...)).
  2. Step 2: Check each option's correctness

    def get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all() uses endswith and all() to get all matches, which is correct. def get_example_users(self): return User.query.filter_by(email='@example.com').all() uses filter_by with exact match, not suffix. def get_example_users(self): return User.query.filter(User.email.contains('@example.com')).first() uses contains but returns only first match. def get_example_users(self): return User.query.filter(User.email == '@example.com').all() uses equality check, not suffix.
  3. Final Answer:

    def get_example_users(self): return User.query.filter(User.email.endswith('@example.com')).all() -> Option C
  4. Quick Check:

    Use endswith() + all() for suffix filtering [OK]
Quick Trick: Use endswith() for suffix filtering in queries [OK]
Common Mistakes:
MISTAKES
  • Using filter_by for partial matches
  • Using contains but returning only first result
  • Using equality operator instead of endswith

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes