﻿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
7099	FieldError in newforms admin when Meta.ordering contains a FK	Ramiro Morales	nobody	"This is the newforms-admin equivalent of #3002. See http://code.djangoproject.com/ticket/3002#comment:3

Find attached patch fixing this (identical modulo lines offsets to the one attached to #3002).

This bug can be reproduced by this minimal app:

{{{
#!python
# models.py

import datetime

from django.db import models
from django.contrib import admin

from admin import PollAdmin, ChoiceAdmin

class Poll(models.Model):
    question = models.CharField(max_length=200)

    def __unicode__(self):
        return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll, null=True)
    choice = models.CharField(max_length=200)

    def __unicode__(self):
        return self.choice

    class Meta:
        ordering = ('poll',)


admin.site.register(Poll, PollAdmin)
admin.site.register(Choice, ChoiceAdmin)
}}}

{{{
#!python
# admin.py

from django.contrib import admin

class PollAdmin(admin.ModelAdmin):
    pass

class ChoiceAdmin(admin.ModelAdmin):
    pass
}}}

Problem shows itself when the ""Choices"" link is clicked in the admin index page. This is the traceback generated:

{{{
#!python
Environment:

Request Method: GET
Request URL: http://ikki:8000/admin/issues/choice/
Django Version: 0.97-newforms-admin-SVN-unknown
Python Version: 2.4.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'nfa_test.issues']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/core/handlers/base.py"" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/sites.py"" in root
  137.                 return self.model_page(request, *url.split('/', 2))
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/sites.py"" in model_page
  154.         return admin_obj(request, rest_of_url)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/options.py"" in __call__
  242.             return self.changelist_view(request)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/options.py"" in changelist_view
  586.                 self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/views/main.py"" in __init__
  148.         self.query_set = self.get_query_set()
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/contrib/admin/views/main.py"" in get_query_set
  309.         qs = qs.order_by(order_type + lookup_order_field)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/db/models/query.py"" in order_by
  427.         obj.query.add_ordering(*field_names)
File ""/mnt/disk2/ramiro/hg-repos/django/t3002-nf-a/django/db/models/sql/query.py"" in add_ordering
  1293.             raise FieldError('Invalid order_by arguments: %s' % errors)

Exception Type: FieldError at /admin/issues/choice/
Exception Value: Invalid order_by arguments: ['issues_poll.id']
}}}

"		closed	contrib.admin	newforms-admin		fixed	changelist newforms-admin FieldEerror fk ordering		Unreviewed	1	0	0	0	0	0
