0Pricing
Django Academy · Lezione

all(), get() e filter()

I metodi fondamentali per recuperare gli oggetti

all(), get() e filter() è una lezione Django 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 Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

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

Meet the Manager

Every model gives you a free objects manager. It is your front door to the database, and every query you write starts from it. 🚪

Book.objects

Grab Everything

Call all() to ask for every row in the table. You get back a QuerySet you can loop over, count, or refine further.

books = Book.objects.all()

A QuerySet Is Not a List

A QuerySet looks list-like but it is lazy. Django waits to hit the database until you actually use the results.

Fetch Exactly One

Use get() when you expect a single matching row, like one record found by its primary key.

book = Book.objects.get(pk=1)

When get() Fails

get() is strict. If nothing matches it raises DoesNotExist, so reach for it only when you are sure one row exists.

Book.objects.get(pk=999)

Too Many Matches

If get() finds more than one row, it raises MultipleObjectsReturned. It truly means one and only one.

Filter for a Subset

Reach for filter() when you want the rows that match a condition. It returns a QuerySet, even if only one row matches.

Book.objects.filter(published=True)

Filter Returns Zero or More

Unlike get(), filter() never raises when nothing matches. You simply get an empty QuerySet back instead of an error.

Book.objects.filter(author="Nobody")

Multiple Conditions

Pass several keyword arguments to filter() and Django joins them with AND. Every condition must be true.

Book.objects.filter(published=True, year=2024)

get vs filter at a Glance

Use get() for one guaranteed object and filter() for a collection. That single choice avoids most beginner query bugs.

Chain Your Filters

Because filter() returns a QuerySet, you can chain calls to narrow results step by step before the database is touched.

Book.objects.filter(published=True).filter(year=2024)

Quick Check

You expect a single book and there is no match. Which call raises an error?

Recap

Nice work! all() grabs everything, get() fetches one exact row, and filter() returns a subset you can chain. 🎉

Domande Frequenti

La lezione «all(), get() e filter()» è gratuita?

Sì — il testo completo di «all(), get() e filter()» è 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 Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «all(), get() e filter()»?

I metodi fondamentali per recuperare gli oggetti Eserciti Django 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 Django Academy?

Non è richiesta alcuna esperienza precedente. Django 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 «all(), get() e filter()»?

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 Django Academy?

Sì. Ogni lezione Django 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. all(), get() e filter()
  2. Lookup dei campi ed exclude()
  3. order_by, slicing e valutazione lazy
  4. values, values_list e count
← Torna a Django Academy