Skip to content

Commit

Permalink
queryset-refactor: Fixed the interaction between extra(select=...) and
Browse files Browse the repository at this point in the history
valuelist(). Fixed #7053.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7446 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Apr 23, 2008
1 parent 42260ef commit 42dc3b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions django/db/models/query.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -557,13 +557,21 @@ def _merge_sanity_check(self, other):


class ValuesListQuerySet(ValuesQuerySet): class ValuesListQuerySet(ValuesQuerySet):
def iterator(self): 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: if self.flat and len(self._fields) == 1:
for row in self.query.results_iter(): for row in self.query.results_iter():
yield row[0] yield row[0]
else: elif not self.query.extra_select:
for row in self.query.results_iter(): for row in self.query.results_iter():
yield row 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): def _clone(self, *args, **kwargs):
clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs) clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)
Expand Down
7 changes: 7 additions & 0 deletions tests/modeltests/lookup/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ def __unicode__(self):
>>> Article.objects.valueslist('id', flat=True).order_by('id') >>> Article.objects.valueslist('id', flat=True).order_by('id')
[1, 2, 3, 4, 5, 6, 7] [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) >>> Article.objects.valueslist('id', 'headline', flat=True)
Traceback (most recent call last): Traceback (most recent call last):
... ...
Expand Down

0 comments on commit 42dc3b9

Please sign in to comment.