0Pricing
Flask Academy · Lesson

Fetch Rows with query and get

Retrieve all, one, or by primary key.

Fetch Rows with query and get is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Reading Starts With query

Every read from your database begins with the model's query attribute. It is your gateway to fetching rows as Python objects. 🔎

rows = User.query

Grab Everything with all

Call all() on a query to load every matching row into a plain Python list you can loop over.

users = User.query.all()
for u in users:
    print(u.name)

Just One With first

Use first() when you want a single row. It returns the first match, or None if the table has nothing to give.

user = User.query.first()

Fetch by Primary Key

When you know the id, get() is the fastest path. Hand it the primary key and it returns that exact row.

user = User.query.get(7)

get Returns None When Missing

If no row has that id, get() quietly returns None instead of raising. Always check before you use the result.

user = User.query.get(999)
if user is None:
    print("not found")

404 the Easy Way

In a view, get_or_404() fetches by id and auto-aborts with a 404 page when the row is absent. No manual check needed.

user = User.query.get_or_404(7)

first_or_404 for Filtered Reads

Pair first_or_404() with a filter to grab one match or raise a clean 404 when nothing fits.

u = User.query.filter_by(email=e).first_or_404()

Count Without Loading Rows

Need a tally, not the data? count() asks the database for the number directly, so you never pull full rows into memory.

total = User.query.count()

Queries Are Lazy

Building a query does not touch the database yet. The SQL only runs when you call a method like all, first, or get.

q = User.query
results = q.all()

Each Row Is an Object

Results come back as real model instances, so you read columns as ordinary attributes like user.name and user.email.

user = User.query.first()
print(user.email)

Pick the Right Fetch

Use get for a known id, first for one of many, and all for the whole set. Choosing well keeps reads clear and fast.

Quick Check

You know a record's primary key. Which call fetches it most directly?

Recap: Fetching Rows

You now read data with query: all for lists, first for one, get by id, and the or_404 helpers for clean views. Nice work! 🎉

Frequently asked questions

Is the “Fetch Rows with query and get” lesson free?

Yes — the full text of “Fetch Rows with query and get” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Fetch Rows with query and get”?

Retrieve all, one, or by primary key. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fetch Rows with query and get” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Fetch Rows with query and get
  2. Filter, Order, and Limit Results
  3. Define Relationships and Foreign Keys
  4. Joins and Lazy vs Eager Loading
← Back to Flask Academy