1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

queryset-refactor: Fixed the interaction between extra(select=...) and

valuelist(). Fixed #7053.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7446 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-04-23 09:53:22 +00:00
parent 42260ef737
commit 42dc3b9ba2
2 changed files with 17 additions and 2 deletions

View File

@ -557,13 +557,21 @@ class ValuesQuerySet(QuerySet):
class ValuesListQuerySet(ValuesQuerySet):
def iterator(self):
self.field_names.extend([f for f in self.query.extra_select.keys()])
self.query.trim_extra_select(self.extra_names)
if self.flat and len(self._fields) == 1:
for row in self.query.results_iter():
yield row[0]
else:
elif not self.query.extra_select:
for row in self.query.results_iter():
yield row
else:
# When extra(select=...) is involved, the extra cols come are
# always at the start of the row, so we need to reorder the fields
# to match the order in self._fields.
names = self.query.extra_select.keys() + self.field_names
for row in self.query.results_iter():
data = dict(zip(names, row))
yield tuple([data[f] for f in self._fields])
def _clone(self, *args, **kwargs):
clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)

View File

@ -180,6 +180,13 @@ True
>>> Article.objects.valueslist('id', flat=True).order_by('id')
[1, 2, 3, 4, 5, 6, 7]
>>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').valueslist('id')
[(1,), (2,), (3,), (4,), (5,), (6,), (7,)]
>>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').valueslist('id_plus_one', 'id')
[(2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6), (8, 7)]
>>> Article.objects.extra(select={'id_plus_one': 'id+1'}).order_by('id').valueslist('id', 'id_plus_one')
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)]
>>> Article.objects.valueslist('id', 'headline', flat=True)
Traceback (most recent call last):
...