﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36606	Optimize QuerySet.values_list(flat=True) with no fields	Adam Johnson		"Currently, `QuerySet.values_list()` ensures that no more than 1 field is set ([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L1417-L1421 source]):

{{{#!python
        if flat and len(fields) > 1:
            raise TypeError(
                ""'flat' is not valid when values_list is called with more than one ""
                ""field.""
            )
}}}

However, it also allows the case where *no* fields are declared, for which all fields are fetched, only to throw away all but the first one ([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L266-L278 source]):

{{{#!python
class FlatValuesListIterable(BaseIterable):
    """"""
    Iterable returned by QuerySet.values_list(flat=True) that yields single
    values.
    """"""

    def __iter__(self):
        queryset = self.queryset
        compiler = queryset.query.get_compiler(queryset.db)
        for row in compiler.results_iter(
            chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
        ):
            yield row[0]
}}}

I think we can optimize this case to select only the first field in the model instead, maintaining semantics while avoiding overfetching.

This case also seems untested with the `values_list()` tests in `tests/lookup/tests.py`, so we'd want to add a test there."	Cleanup/optimization	new	Database layer (models, ORM)	dev	Normal				Unreviewed	0	0	0	0	0	0
