By default, Django ORM issues SELECT * on every column, even when you only need one or two columns for display. The fix: use values() when you need a list of dicts, and values_list() when you need tuples or a flat list — the queryset then SELECTs exactly the columns you list, using less RAM and less data transferred over the network. This applies to any read-only queryset (dropdowns, API list endpoints, reports) — but keep full objects when you need to call model methods or save().

The Problem

A very familiar scenario: you need to fill a filter dropdown with product names, or return a name list for a search-suggestion API. The Product table has a TEXT description column, each row tens of KB, and you have 10,000 products. Code many people write out of habit:

# You only need names for the dropdown...
products = Product.objects.all() # SELECT * — pulls description x 10,000 rows
names = [p.name for p in products]

This statement SELECTs every column of the table, builds 10,000 full Product objects in RAM, and then you only read .name. It wastes memory and time transferring data from the database to the app — while the actual need is a single column.

The Solution

Fetch exactly the columns you need, no more:

# Just need a list of names:
names = Product.objects.values_list("name", flat=True)
# SQL: SELECT "name" FROM shop_product
# → ['Ballpoint pen', 'Ruled notebook']
# Need prices displayed alongside:
items = Product.objects.values("name", "price")
# SQL: SELECT "name", "price" FROM shop_product
# → [{'name': 'Ballpoint pen', 'price': 5000}, ...]

Why this is better:

  • values() returns dicts keyed by column name — handy when you need JSON output or key-based template rendering.
  • values_list() returns tuples — most compact for dropdowns, autocomplete, or passing into the __in of another query.
  • Add flat=True when fetching exactly 1 column to get a flat list instead of a list of 1-element tuples; add named=True to get namedtuples and access like row.name.
  • Still a lazy queryset: keep chaining .filter(), .order_by() normally, and it combines very well with annotate() for group-by reports right in the database.

Apply It to Your Project

Rule of thumb: if the queryset is read-only and you don’t need to call model methods (like get_absolute_url()) or save() it back — use values()/values_list() from the start. The places that benefit most are search-suggestion boxes, filter dropdowns, compact list endpoints for the frontend, and any aggregation reports. When you need full objects to edit and save, but still want to cut the SELECTed columns, use only()/defer() instead of values — because that object must remain a model instance.

Related Posts

Frequently Asked Questions

Is values() still a queryset? Is it lazy?

Yes. Both return a lazy QuerySet like normal — the database isn’t touched until you iterate, call list(), or len(). You can still chain filter(), exclude(), and order_by() freely.

Can I still get N+1 errors with values()?

Yes, if you list columns of a related table and keep accessing it in a loop. values() only reduces the SELECTed columns; it doesn’t replace select_related()/prefetch_related(). Combine both when the query touches relationships.

When should I NOT use values()/values_list()?

When you need to call model methods, need to save() the object back, or your template/form expects a model instance. Then use full objects, and cut columns with only()/defer() if needed.

When do I use flat=True and named=True?

flat=True is only for values_list() with exactly 1 column — it returns a flat list like ['Ballpoint pen', 'Ruled notebook'], very handy for dropdowns. named=True returns namedtuples for row.name-style access instead of row[0], making the code more readable when fetching many columns.

References