diff --git a/django/db/models/query.py b/django/db/models/query.py index 12731caa94..fb6d116a6e 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -513,7 +513,9 @@ class ValuesQuerySet(QuerySet): # names of the model fields to select. def iterator(self): - self.query.trim_extra_select(self.extra_names) + if (not self.extra_names and + len(self.field_names) != len(self.model._meta.fields)): + self.query.trim_extra_select(self.extra_names) names = self.query.extra_select.keys() + self.field_names for row in self.query.results_iter(): yield dict(zip(names, row)) diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 8c9334c849..aa78d6583a 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -507,6 +507,12 @@ True >>> [sorted(d.items()) for d in dicts] [[('id', 1), ('rank', 2)], [('id', 2), ('rank', 1)], [('id', 3), ('rank', 3)]] +Bug #7256 +# An empty values() call includes all aliases, including those from an extra() +>>> dicts = qs.values().order_by('id') +>>> [sorted(d.items()) for d in dicts] +[[('author_id', 2), ('good', 0), ('id', 1), ('rank', 2)], [('author_id', 3), ('good', 0), ('id', 2), ('rank', 1)], [('author_id', 1), ('good', 1), ('id', 3), ('rank', 3)]] + Bugs #2874, #3002 >>> qs = Item.objects.select_related().order_by('note__note', 'name') >>> list(qs)