Skip to content

Commit 2063c88

Browse files
adamchainzjacobtylerwalls
authored andcommitted
Fixed #36606 -- Optimized QuerySet.values_list(flat=True) without fields.
1 parent 2336d5d commit 2063c88

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

django/db/models/query.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,11 +1416,14 @@ def values(self, *fields, **expressions):
14161416
def values_list(self, *fields, flat=False, named=False):
14171417
if flat and named:
14181418
raise TypeError("'flat' and 'named' can't be used together.")
1419-
if flat and len(fields) > 1:
1420-
raise TypeError(
1421-
"'flat' is not valid when values_list is called with more than one "
1422-
"field."
1423-
)
1419+
if flat:
1420+
if len(fields) > 1:
1421+
raise TypeError(
1422+
"'flat' is not valid when values_list is called with more than one "
1423+
"field."
1424+
)
1425+
elif not fields:
1426+
fields = [self.model._meta.concrete_fields[0].attname]
14241427

14251428
field_names = {f: False for f in fields if not hasattr(f, "resolve_expression")}
14261429
_fields = []

0 commit comments

Comments
 (0)