values, values_list und count
Rufen Sie kompakte Daten und Aggregationen ab.
values, values_list und count ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Go Lightweight
Full model objects carry every field. When you only need a column or two, lighter tools fetch less data and run faster.
values() Returns Dicts
Call values() and each row comes back as a dictionary instead of a model instance. Handy for JSON and quick reads.
Book.objects.values("title", "year")Pick Only What You Need
Name the fields you pass to values() and Django selects just those columns, keeping each dictionary small and focused.
Book.objects.values("title")values_list() Returns Tuples
Prefer tuples? values_list() gives each row as a tuple of values in the order you named the fields.
Book.objects.values_list("title", "year")Flatten a Single Column
Asking for one field? Add flat=True to get a clean list of plain values instead of one-item tuples.
Book.objects.values_list("title", flat=True)flat Needs One Field
Remember, flat=True only works with a single field. Pass two fields and Django raises an error to keep things clear.
These Are Still QuerySets
Both values() and values_list() stay lazy QuerySets, so you can filter and order them before they ever run.
Book.objects.filter(published=True).values("title")Count Without Loading
Need just a total? count() asks the database to count rows directly, far cheaper than pulling them all into Python.
Book.objects.filter(published=True).count()count vs len
Use count() when you only need the number. Calling len() first loads every object, then counts in memory.
Does Anything Match
To check if rows exist, exists() beats count(). It stops at the first match and returns a simple True or False.
Book.objects.filter(year=2024).exists()Combine for Clean Reads
Mix these freely: filter to narrow, then values_list for a tidy list or count for a total. Each query stays lean.
Book.objects.filter(published=True).values_list("id", flat=True)Quick Check
You want a plain list of every book title, not tuples or dictionaries. What do you use?
Recap
Wonderful! values() gives dicts, values_list() gives tuples, and count() totals rows in the database. 📊
Lerne Python mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 30
- Lektionen
- 120
Häufig gestellte Fragen
Ist die Lektion „values, values_list und count“ kostenlos?
Ja — der vollständige Text von „values, values_list und count“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „values, values_list und count“?
Rufen Sie kompakte Daten und Aggregationen ab. Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „values, values_list und count“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- all(), get() und filter()
- Feld-Lookups und exclude()
- order_by, Slicing und Lazy Evaluation
- values, values_list und count