Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#8634 closed (fixed)

Wrong ordering by a field in a different table example in new (development ) queryset-api documentation

Reported by: Jerzy.Ludwichowski@… Owned by: ramiro
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


The example on ordering a queryset by a field in a different table in the development documentation on queryset-api ( offers the following syntax:

Entry.objects.order_by('', 'headline')

preceded by the following explanation: "To order by a field in a different table, add the other table’s name and a dot, like so:"

For the following model definitions

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choices = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choices

with MySQL, for this retrieval example:


the result is "OperationalError: (1054, "Unknown column 'polls_poll.question' in 'order clause'" in reply to the following SQL query generated:

SELECT `polls_choice`.`id`, `polls_choice`.`poll_id`, `polls_choice`.`choices`,
`polls_choice`.`votes` FROM `polls_choice` ORDER BY `polls_poll`.question ASC,
 `polls_choice`.`votes` DESC

On the other hand
if one follows the syntax offered in the "mainstream" documentation (, i.e.,


things do work, with the following SQL query generated:

SELECT `polls_choice`.`id`, `polls_choice`.`poll_id`, `polls_choice`.`choices`,
`polls_choice`.`votes` FROM `polls_choice` INNER JOIN `polls_poll` 
ON (`polls_choice`.`poll_id` = `polls_poll`.`id`) ORDER BY `polls_poll`.`question` ASC, 
`polls_choice`.`votes` DESC

This problem might be related to the one reported in the accepted ticket #2884 (component: Admin interface), solution for which is said to be deferred until after queryset-refactor.

Attachments (1)

t8634.diff (2.6 KB) - added by ramiro 7 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 7 years ago by Jerzy.Ludwichowski@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Further to the report: I'm running on the development code (rev 8649).

comment:2 Changed 7 years ago by ramiro

  • milestone set to 1.0
  • Owner changed from nobody to ramiro
  • Status changed from new to assigned

Correct. This is a regression caused by the landing of the documentation refactor (doc-rf) in trunk [8506]
The pre doc-rf documentation about this topic is correct:

Accepting the ticket and targeting it to 1.0.

comment:3 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

Changed 7 years ago by ramiro

comment:4 Changed 7 years ago by ramiro

  • Has patch set

comment:5 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

Fixed in [8694]

comment:6 Changed 4 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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