﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29538	QueryExpression in ordering of a related object fails	wilhelmhb	nobody	"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."	Bug	new	Database layer (models, ORM)	2.0	Normal		ordering, query expression, related object		Unreviewed	0	0	0	0	0	0
