#16706: values_list on query set ordered by field added by QuerySet.extra() 
broken
-----------------------+----------------------------------------------
 Reporter:  salfner@…  |          Owner:  nobody
     Type:  Bug        |         Status:  new
Milestone:             |      Component:  Database layer (models, ORM)
  Version:  1.3        |       Severity:  Normal
 Keywords:             |   Triage Stage:  Unreviewed
Has patch:  0          |  Easy pickings:  0
    UI/UX:  0          |
-----------------------+----------------------------------------------
 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
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16706>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to