Opened 13 years ago

Closed 12 years ago

#16706 closed Bug (duplicate)

values_list on query set ordered by field added by QuerySet.extra() broken

Reported by: salfner@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (3)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

Indeed, the combination of extra, order_by and values/values_list triggers this exception.

comment:2 by anonymous, 12 years ago

The same code seems to work in Django 1.4.1

comment:3 by Craig de Stigter, 12 years ago

Resolution: duplicate
Status: newclosed

duplicate of #14930

Note: See TracTickets for help on using tickets.
Back to Top