| 1515 | return clone |
| 1516 | |
| 1517 | def values_list(self, *fields, flat=False, named=False): |
| 1518 | if flat and named: |
| 1519 | raise TypeError("'flat' and 'named' can't be used together.") |
| 1520 | if flat: |
| 1521 | if len(fields) > 1: |
| 1522 | raise TypeError( |
| 1523 | "'flat' is not valid when values_list is called with more than one " |
| 1524 | "field." |
| 1525 | ) |
| 1526 | elif not fields: |
| 1527 | # RemovedInDjango70Warning: When the deprecation ends, replace |
| 1528 | # with: |
| 1529 | # raise TypeError( |
| 1530 | # "'flat' is not valid when values_list is called with no " |
| 1531 | # "fields." |
| 1532 | # ) |
| 1533 | warnings.warn( |
| 1534 | "Calling values_list() with no field name and flat=True " |
| 1535 | "is deprecated. Pass an explicit field name instead, like " |
| 1536 | "'pk'.", |
| 1537 | RemovedInDjango70Warning, |
| 1538 | ) |
| 1539 | fields = [self.model._meta.concrete_fields[0].attname] |
| 1540 | |
| 1541 | field_names = {f: False for f in fields if not hasattr(f, "resolve_expression")} |
| 1542 | _fields = [] |
| 1543 | expressions = {} |
| 1544 | counter = 1 |
| 1545 | for field in fields: |
| 1546 | field_name = field |
| 1547 | expression = None |
| 1548 | if hasattr(field, "resolve_expression"): |
| 1549 | field_name = getattr( |
| 1550 | field, "default_alias", field.__class__.__name__.lower() |
| 1551 | ) |
| 1552 | expression = field |
| 1553 | # For backward compatibility reasons expressions are always |
| 1554 | # prefixed with the counter even if their default alias doesn't |
| 1555 | # collide with field names. Changing this logic could break |
| 1556 | # some usage of named=True. |
| 1557 | seen = True |
| 1558 | elif seen := field_names[field_name]: |
| 1559 | expression = F(field_name) |
| 1560 | if seen: |
| 1561 | field_name_prefix = field_name |
| 1562 | while (field_name := f"{field_name_prefix}{counter}") in field_names: |
| 1563 | counter += 1 |
| 1564 | if expression is not None: |
| 1565 | expressions[field_name] = expression |
| 1566 | field_names[field_name] = True |
| 1567 | _fields.append(field_name) |
| 1568 | |
| 1569 | clone = self._values(*_fields, **expressions) |
| 1570 | clone._iterable_class = ( |
| 1571 | NamedValuesListIterable |
| 1572 | if named |
| 1573 | else FlatValuesListIterable if flat else ValuesListIterable |
| 1574 | ) |