What's the difference between `values()`, `values_list()`, and model instances?
The difference between Django values, valueslist and model instances, explained by what each row costs. A plain queryset selects every concrete column and constructs a model instance per row, which is what makes methods, properties and save available. values narrows the SELECT to the named fields and yields plain dicts, valueslist yields tuples of the same data, flat=True unwraps a single field to bare scalars, and named=True gives lightweight named tuples with attribute access but no model. Shows why a dict cannot use a model property even when it holds the underlying column, since the behaviour lives on the class, and why only() sits between the two, returning real instances from a narrowed query but paying an extra query per instance when a deferred field is touched. Explains why values placed before annotate stops being a projection and becomes the GROUP BY key. Includes runnable Django code and real output, plus relation traversal and aggregate granularity.
Comentarii