Querying with filter and filter_by in Flask
📖 Scenario: You are building a simple Flask app to manage a list of books in a library. You want to find books by their author or by their publication year using Flask-SQLAlchemy queries.
🎯 Goal: Learn how to use filter and filter_by methods in Flask-SQLAlchemy to query the database for specific books.
📋 What You'll Learn
Create a
Book model with id, title, author, and year fieldsAdd a configuration variable
search_year with the value 2020Write a query using
filter_by to find books by author 'Alice'Write a query using
filter to find books published after search_year💡 Why This Matters
🌍 Real World
Filtering database records is common in web apps to show users only relevant data, like books by a certain author or recent publications.
💼 Career
Knowing how to query databases efficiently with filter and filter_by is essential for backend web developers working with Flask and SQL databases.
Progress0 / 4 steps