0Pricing
Flask Academy · Lezione

Recuperare righe con query e get

Recuperi tutte le righe, una sola o quella con una determinata chiave primaria.

Recuperare righe con query e get è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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! 🎉

Domande Frequenti

La lezione «Recuperare righe con query e get» è gratuita?

Sì — il testo completo di «Recuperare righe con query e get» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Recuperare righe con query e get»?

Recuperi tutte le righe, una sola o quella con una determinata chiave primaria. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Recuperare righe con query e get»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Recuperare righe con query e get
  2. Filtrare, ordinare e limitare i risultati
  3. Definire relazioni e chiavi esterne
  4. Join e caricamento lazy o eager
← Torna a Flask Academy