[Django] #29272: Queryset using order_by(F()) fails to compile with values_list

7 views
Skip to first unread message

Django

unread,
Mar 29, 2018, 5:36:12 AM3/29/18
to django-...@googlegroups.com
#29272: Queryset using order_by(F()) fails to compile with values_list
-------------------------------------+-------------------------------------
Reporter: bszfw | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Here is a simplified version of what I am trying to do:

{{{
from django.db import models
from django.db.models import IntegerField, F, Value

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

Question.objects.annotate(test=Value(1,
output_field=IntegerField())).order_by(F('test')).values_list('question_text')
}}}

It works without using {{{F}}}, or by adding the annotated value to
{{{values_list}}}.
But I need {{{F}}} to be able to write
{{{F('test').asc(nulls_last=True)}}}, and I cannot modify
{{{values_list}}} as it is added to the Queryset later in code which does
not know anything about the annotation.

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

Django

unread,
Mar 29, 2018, 3:40:20 PM3/29/18
to django-...@googlegroups.com
#29272: Queryset using order_by(F()) fails to compile with values_list
-------------------------------------+-------------------------------------
Reporter: bszfw | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Could you possibly provide the exact traceback you encounter?

Also you should be able to pass your expression directly to `order_by`


{{{#!python
Question.objects.order_by(
Value(1, output_field=IntegerField()).asc(nulls_last=True),
).values_list('question_text')
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29272#comment:1>

Django

unread,
Apr 2, 2018, 12:18:23 PM4/2/18
to django-...@googlegroups.com
#29272: Queryset using order_by(F()) fails to compile with values_list
-------------------------------------+-------------------------------------
Reporter: bszfw | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


Comment:

The traceback:
{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "django/db/models/query.py", line 248, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "django/db/models/query.py", line 272, in __iter__
self._fetch_all()
File "django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "django/db/models/query.py", line 138, in __iter__
return compiler.results_iter(tuple_expected=True,
chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "django/db/models/sql/compiler.py", line 1018, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch,
chunk_size=chunk_size)
File "django/db/models/sql/compiler.py", line 1054, in execute_sql
sql, params = self.as_sql()
File "django/db/models/sql/compiler.py", line 447, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "django/db/models/sql/compiler.py", line 51, in pre_sql_setup
order_by = self.get_order_by()
File "django/db/models/sql/compiler.py", line 346, in get_order_by
self.query, allow_joins=True, reuse=None)
File "django/db/models/expressions.py", line 248, in resolve_expression
for expr in c.get_source_expressions()
File "django/db/models/expressions.py", line 248, in <listcomp>
for expr in c.get_source_expressions()
File "django/db/models/expressions.py", line 516, in resolve_expression
return query.resolve_ref(self.name, allow_joins, reuse, summarize)
File "django/db/models/sql/query.py", line 1518, in resolve_ref
return self.annotation_select[name]
KeyError: 'test'
}}}
Duplicate of #28811 which will be fixed in Django 2.1.

--
Ticket URL: <https://code.djangoproject.com/ticket/29272#comment:2>

Reply all
Reply to author
Forward
0 new messages