﻿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
3756	ordering with two levels of ForeignKey in the admin interface	mssnlayam@…	nobody	"I don't know if this bug is the same as #3002, #2076. I am reporting this nevertheless.

The Django admin throws an exception with the following code when viewing the Paragraph table.

{{{
class Reporter(models.Model):
    name = models.CharField(maxlength=30)

class Article(models.Model):
    name = models.CharField(maxlength=30)
    reporter = models.ForeignKey(Reporter)

    class Meta:
        ordering = ('reporter',)

class Paragraph(models.Model):
    name = models.CharField(maxlength=30)
    article = models.ForeignKey(Article)

    class Admin:
        list_display = ('name', 'article')

    class Meta:
        ordering = ('article',)
}}}

Here is the traceback

{{{
Traceback (most recent call last):
File ""/tmp/foo/django/django-src/django/core/handlers/base.py"" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File ""/tmp/foo/django/django-src/django/contrib/admin/views/decorators.py"" in _checklogin
  55. return view_func(request, *args, **kwargs)
File ""/tmp/foo/django/django-src/django/views/decorators/cache.py"" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File ""/tmp/foo/django/django-src/django/contrib/admin/views/main.py"" in change_list
  760. cl = ChangeList(request, model)
File ""/tmp/foo/django/django-src/django/contrib/admin/views/main.py"" in __init__
  572. self.get_results(request)
File ""/tmp/foo/django/django-src/django/contrib/admin/views/main.py"" in get_results
  630. result_list = list(self.query_set)
File ""/tmp/foo/django/django-src/django/db/models/query.py"" in __iter__
  108. return iter(self._get_data())
File ""/tmp/foo/django/django-src/django/db/models/query.py"" in _get_data
  468. self._result_cache = list(self.iterator())
File ""/tmp/foo/django/django-src/django/db/models/query.py"" in iterator
  181. cursor.execute(""SELECT "" + (self._distinct and ""DISTINCT "" or """") + "","".join(select) + sql, params)
File ""/tmp/foo/django/django-src/django/db/backends/util.py"" in execute
  12. return self.cursor.execute(sql, params)
File ""/tmp/foo/django/django-src/django/db/backends/sqlite3/base.py"" in execute
  93. return Database.Cursor.execute(self, query, params)

  OperationalError at /admin/foo/paragraph/
  no such column: foo_article.reporter
}}}

The offending query is
{{{
SELECT
    ""foo_paragraph"".""id"",
    ""foo_paragraph"".""name"",
    ""foo_paragraph"".""article_id"",
    ""foo_article"".""id"",
    ""foo_article"".""name"",
    ""foo_article"".""reporter_id"",
    ""foo_reporter"".""id"",
    ""foo_reporter"".""name""
FROM
    ""foo_paragraph"" , ""foo_article"", ""foo_reporter""
WHERE
    ""foo_paragraph"".""article_id"" = ""foo_article"".""id"" AND
    ""foo_article"".""reporter_id"" = ""foo_reporter"".""id""
ORDER BY
    ""foo_article"".""reporter"" ASC
}}}

There is no column foo_article.reporter. The column name is foo_article.reporter_id."		closed	contrib.admin	dev		duplicate		mssnlayam@…	Unreviewed	0	0	0	0	0	0
