[Django] #29538: QueryExpression in ordering of a related object fails

39 views
Skip to first unread message

Django

unread,
Jul 1, 2018, 2:25:36 PM7/1/18
to django-...@googlegroups.com
#29538: QueryExpression in ordering of a related object fails
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) | Keywords: ordering, query
Severity: Normal | expression, related object
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Since 2.0, according to the doc
([https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering]), we
can use QueryExpression objects in the Model.Meta.ordering field.
Using:

{{{
from django.db import models

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100, null=True, blank=True)

class Meta:
ordering = [models.F('instrument').asc(null_last=True)]

class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()

class Meta:
ordering = ['artist']
}}}
fails with {{{ TypeError: 'OrderBy' does not support indexing }}}

When reaching
[https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L669],
the compiler tries to use the related model, but at line 679, {{{item}}}
can be an OrderBy object. Thus the failure.

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

Django

unread,
Jul 1, 2018, 2:26:50 PM7/1/18
to django-...@googlegroups.com
#29538: QueryExpression in ordering of a related object fails
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage:
expression, related object | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: wilhelmhb (added)


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

Django

unread,
Jul 3, 2018, 10:40:33 AM7/3/18
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails

-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 0 | Needs documentation: 0

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

* stage: Unreviewed => Accepted


Old description:

> Since 2.0, according to the doc
> ([https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering]),
> we can use QueryExpression objects in the Model.Meta.ordering field.
> Using:
>
> {{{
> from django.db import models
>
> class Musician(models.Model):
> first_name = models.CharField(max_length=50)
> last_name = models.CharField(max_length=50)
> instrument = models.CharField(max_length=100, null=True, blank=True)
>
> class Meta:
> ordering = [models.F('instrument').asc(null_last=True)]
>
> class Album(models.Model):
> artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
> name = models.CharField(max_length=100)
> release_date = models.DateField()
> num_stars = models.IntegerField()
>
> class Meta:
> ordering = ['artist']
> }}}
> fails with {{{ TypeError: 'OrderBy' does not support indexing }}}
>
> When reaching
> [https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L669],
> the compiler tries to use the related model, but at line 679, {{{item}}}
> can be an OrderBy object. Thus the failure.

New description:

Since 2.0, according to the doc
([https://docs.djangoproject.com/en/2.0/ref/models/options/#ordering]), we
can use QueryExpression objects in the Model.Meta.ordering field.
Using:

{{{
from django.db import models

class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100, null=True, blank=True)

class Meta:
ordering = [models.F('instrument').asc(nulls_last=True)]

class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()

class Meta:
ordering = ['artist']
}}}

{{{
>>> Album.objects.all()
...


TypeError: 'OrderBy' does not support indexing
}}}

When reaching
[https://github.com/django/django/blob/master/django/db/models/sql/compiler.py#L669],
the compiler tries to use the related model, but at line 679, {{{item}}}
can be an OrderBy object. Thus the failure.

--

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

Django

unread,
Jul 7, 2018, 7:19:59 PM7/7/18
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 0 | Needs documentation: 0

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

* status: new => assigned
* owner: nobody => Alexandre Laplante


Comment:

Started working on a fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:3>

Django

unread,
Jul 7, 2018, 10:51:42 PM7/7/18
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/10146 PR] Looking for feedback on
this patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:4>

Django

unread,
Sep 12, 2018, 5:03:22 AM9/12/18
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:5>

Django

unread,
Jan 14, 2019, 8:38:57 AM1/14/19
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by colons):

* cc: colons (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:6>

Django

unread,
May 2, 2022, 7:49:44 AM5/2/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

After 8c5f9906c56ac72fc4f13218dd90bdf9bc8a248b it crashes with:
{{{
django.core.exceptions.FieldError: Cannot resolve keyword 'instrument'
into field. Choices are: artist, artist_id, id, name, num_stars,
release_date
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:7>

Django

unread,
May 4, 2022, 12:20:50 AM5/4/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

#33678 was a duplicate for functions in related `Meta.ordering`.

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:8>

Django

unread,
May 4, 2022, 10:58:02 AM5/4/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

If anyone is interested in addressing this ticket there's a possible
implementation detailed in
https://code.djangoproject.com/ticket/33678#comment:3

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:9>

Django

unread,
May 5, 2022, 12:48:51 PM5/5/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Alexandre
| Laplante
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0

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

* cc: Eduardo Rivas (added)
* needs_better_patch: 1 => 0


Comment:

[https://github.com/django/django/pull/15666 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:10>

Django

unread,
May 5, 2022, 12:49:50 PM5/5/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: Eduardo
| Rivas

Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0

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

* owner: Alexandre Laplante => Eduardo Rivas


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:11>

Django

unread,
May 6, 2022, 12:51:33 AM5/6/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:12>

Django

unread,
May 10, 2022, 10:32:29 AM5/10/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Accepted
expression, related object |
Has patch: 1 | Needs documentation: 0

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

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:13>

Django

unread,
May 12, 2022, 1:25:34 AM5/12/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ordering, query | Triage Stage: Ready for
expression, related object | checkin
Has patch: 1 | Needs documentation: 0

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

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:14>

Django

unread,
May 12, 2022, 2:51:04 AM5/12/22
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: closed

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: ordering, query | Triage Stage: Ready for
expression, related object | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"2798c937deb6625a4e6a36e70d4d60ce5faac954" 2798c937]:
{{{
#!CommitTicketReference repository=""
revision="2798c937deb6625a4e6a36e70d4d60ce5faac954"
Fixed #29538 -- Fixed crash of ordering by related fields when
Meta.ordering contains expressions.

Thanks Simon Charette for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:15>

Django

unread,
Mar 28, 2023, 11:08:27 AM3/28/23
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: ordering, query | Triage Stage: Ready for
expression, related object | checkin
Has patch: 1 | Needs documentation: 0

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

Comment (by a-p-f):

Sorry for my ignorance, but is there a simple way for users to see, from
this page, which minor versions of Django have received this patch? I
followed the link to the github commit page, and from what I could tell
there, the fix was implemented in 4.1. That doesn't tell me, however, if
the commit was cherry-picked into any other branches.

At first I had assumed that since 3.2 was the latest LTS release, it would
receive this fix. I upgraded to 3.2.18, though, and this bug isn't fixed.
Looking closer at Django's docs, I see that 3.2 is only in extended
support, and this bug does not qualify for extended support. Fair enough.
I just think it would help developers to make it clear which of the
current versions have received a given fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:16>

Django

unread,
Mar 28, 2023, 11:40:27 AM3/28/23
to django-...@googlegroups.com
#29538: Query Expression in ordering of a related object fails
-------------------------------------+-------------------------------------

Reporter: wilhelmhb | Owner: Eduardo
| Rivas
Type: Bug | Status: closed
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: ordering, query | Triage Stage: Ready for
expression, related object | checkin
Has patch: 1 | Needs documentation: 0

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

Comment (by Mariusz Felisiak):

Replying to [comment:16 a-p-f]:


> Sorry for my ignorance, but is there a simple way for users to see, from
this page, which minor versions of Django have received this patch? I
followed the link to the github commit page, and from what I could tell
there, the fix was implemented in 4.1. That doesn't tell me, however, if
the commit was cherry-picked into any other branches.
>
> At first I had assumed that since 3.2 was the latest LTS release, it
would receive this fix. I upgraded to 3.2.18, though, and this bug isn't
fixed. Looking closer at Django's docs, I see that 3.2 is only in extended
support, and this bug does not qualify for extended support. Fair enough.
I just think it would help developers to make it clear which of the
current versions have received a given fix.

You can click commit (2798c937deb6625a4e6a36e70d4d60ce5faac954) which
fixed this issue and check tags, `4.1a1` so it's fixed in Django 4.1+.

--
Ticket URL: <https://code.djangoproject.com/ticket/29538#comment:17>

Reply all
Reply to author
Forward
0 new messages