Opened 4 years ago

Closed 5 months ago

#29538 closed Bug (fixed)

Query Expression in ordering of a related object fails

Reported by: wilhelmhb Owned by: Eduardo Rivas
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords: ordering, query expression, related object
Cc: wilhelmhb, colons, Eduardo Rivas Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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.

Change History (15)

comment:1 Changed 4 years ago by wilhelmhb

Cc: wilhelmhb added

comment:2 Changed 4 years ago by Tim Graham

Description: modified (diff)
Summary: QueryExpression in ordering of a related object failsQuery Expression in ordering of a related object fails
Triage Stage: UnreviewedAccepted

comment:3 Changed 4 years ago by Alexandre Laplante

Owner: changed from nobody to Alexandre Laplante
Status: newassigned

Started working on a fix.

comment:4 Changed 4 years ago by Alexandre Laplante

Has patch: set

PR Looking for feedback on this patch.

comment:5 Changed 4 years ago by Carlton Gibson

Patch needs improvement: set

comment:6 Changed 4 years ago by colons

Cc: colons added

comment:7 Changed 5 months ago 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

comment:8 Changed 5 months ago by Mariusz Felisiak

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

comment:9 Changed 5 months ago 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

comment:10 Changed 5 months ago by Eduardo Rivas

Cc: Eduardo Rivas added
Patch needs improvement: unset

comment:11 Changed 5 months ago by Eduardo Rivas

Owner: changed from Alexandre Laplante to Eduardo Rivas

comment:12 Changed 5 months ago by Mariusz Felisiak

Needs tests: set

comment:13 Changed 5 months ago by Simon Charette

Needs tests: unset

comment:14 Changed 5 months ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:15 Changed 5 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 2798c937:

Fixed #29538 -- Fixed crash of ordering by related fields when Meta.ordering contains expressions.

Thanks Simon Charette for the review.

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