﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16706	values_list on query set ordered by field added by QuerySet.extra() broken	salfner@…	nobody	"Hi,

calling values_list() of a QuerySet object that has an ""order_by"" on a field that has been added by extra(select={...}) results in an error.

To reproduce the bug, use the following (pointless) example code:


{{{
from django.contrib.auth.models import User
qs = User.objects.extra(select={'sortfield':'select username'}).order_by('sortfield')
print qs.values_list('pk')

FieldError: Cannot resolve keyword 'sortfield' into field. Choices are: _message_set, auth_tokens, date_joined, email, files, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, password, properties, user_permissions, username

}}}


However, the following works and yields the correctly ordered result:
{{{
for user in qs.all():
  print user.pk, user.sortfield
}}}

Also the SQL statement obtained by:
{{{
print qs.query

SELECT (select username) AS ""sortfield"", ""auth_user"".""id"", ""auth_user"".""username"", ""auth_user"".""first_name"", ""auth_user"".""last_name"", ""auth_user"".""email"", ""auth_user"".""password"", ""auth_user"".""is_staff"", ""auth_user"".""is_active"", ""auth_user"".""is_superuser"", ""auth_user"".""last_login"", ""auth_user"".""date_joined"" FROM ""auth_user"" ORDER BY ""sortfield"" ASC
}}}
executes correctly on the database console (SQLite in this case)

The full stack trace of the error is:
{{{
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\query.py"", line 69, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\query.py"", line 84, in __len__
    self._result_cache.extend(self._iter)
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\query.py"", line 959, in iterator
    for row in self.query.get_compiler(self.db).results_iter():
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\compiler.py"", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\compiler.py"", line 725, in execute_sql
    sql, params = self.as_sql()
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\compiler.py"", line 60, in as_sql
    ordering, ordering_group_by = self.get_ordering()
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\compiler.py"", line 349, in get_ordering
    self.query.model._meta, default_order=asc):
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\compiler.py"", line 378, in find_ordering_name
    opts, alias, False)
  File ""C:\Program Files (x86)\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\query.py"", line 1238, in setup_joins
    ""Choices are: %s"" % (name, "", "".join(names)))
FieldError: Cannot resolve keyword 'sortfield' into field. Choices are: _message_set, auth_tokens, date_joined, email, files, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, password, properties, user_permissions, username
}}}"	Bug	closed	Database layer (models, ORM)	1.3	Normal	duplicate			Accepted	0	0	0	0	0	0
