Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug with using values() and extra(select) in the same QuerySet,…

… with a select dictionary containing more than a few elements. This bug was identified in unit test from [5767]. The problem was that we were relying on the dictionary's .items() ordering, which is undefined

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5768 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5b8d2c9f0df1dbfadafeea6420fc5fc175e00376 1 parent 8831f3e
@adrianholovaty adrianholovaty authored
Showing with 17 additions and 9 deletions.
  1. +17 −9 django/db/models/query.py
View
26 django/db/models/query.py
@@ -579,28 +579,36 @@ def iterator(self):
except EmptyResultSet:
raise StopIteration
- # self._fields is a list of field names to fetch.
+ # self._select is a dictionary, and dictionaries' key order is
+ # undefined, so we convert it to a list of tuples.
+ extra_select = self._select.items()
+
+ # Construct two objects -- fields and field_names.
+ # fields is a list of Field objects to fetch.
+ # field_names is a list of field names, which will be the keys in the
+ # resulting dictionaries.
if self._fields:
- if not self._select:
+ if not extra_select:
fields = [self.model._meta.get_field(f, many_to_many=False) for f in self._fields]
+ field_names = self._fields
else:
fields = []
+ field_names = []
for f in self._fields:
if f in [field.name for field in self.model._meta.fields]:
fields.append(self.model._meta.get_field(f, many_to_many=False))
- elif not self._select.has_key( f ):
- raise FieldDoesNotExist, '%s has no field named %r' % ( self.model._meta.object_name, f )
-
- field_names = self._fields
+ field_names.append(f)
+ elif not self._select.has_key(f):
+ raise FieldDoesNotExist('%s has no field named %r' % (self.model._meta.object_name, f))
else: # Default to all fields.
fields = self.model._meta.fields
field_names = [f.attname for f in fields]
columns = [f.column for f in fields]
select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns]
- # Add any additional SELECTs.
- if self._select:
- select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()])
+ if extra_select:
+ select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in extra_select])
+ field_names.extend([f[0] for f in extra_select])
cursor = connection.cursor()
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
Please sign in to comment.
Something went wrong with that request. Please try again.