Ordering by foreign key results in FieldDoesNotExist-Error

10 views
Skip to first unread message

Andreas

unread,
Sep 13, 2005, 8:24:59 AM9/13/05
to Django users
I'm trying to order a class by its parent, but apparently I'm doing
something wrong. The code I use:


class ParentClass(meta.Model):
somevar = meta.CharField(maxlength=100)
def __repr__(self):
return self.somevar
class META:
admin = meta.Admin()
ordering = ['somevar']

class FirstChild(meta.Model):
theparent = meta.ForeignKey(ParentClass)
myvar = meta.CharField(maxlength=100)
def __repr__(self):
return self.myvar
class META:
admin = meta.Admin()
order_with_respect_to = 'theparent'


I can add ParentClass- and FirstChild-Items in Admin and viewing the
ParentClass-List works, too. Viewing the FirstChild-List, however,
results in an error:


There's been an error:

Traceback (most recent call last):

File
"C:\Programme\Python24\lib\site-packages\django\core\handlers\base.py",
line 64, in get_response
response = callback(request, **param_dict)

File
"C:\Programme\Python24\lib\site-packages\django\views\admin\main.py",
line 132, in change_list
if isinstance(lookup_opts.get_field(order_field).rel,
meta.ManyToOne):

File
"C:\Programme\Python24\lib\site-packages\django\core\meta\__init__.py",
line 252, in get_field
raise FieldDoesNotExist, "name=%s" % name

FieldDoesNotExist: name=_order


Any ideas how I can get that to work?

Andreas

Jacob Kaplan-Moss

unread,
Sep 13, 2005, 8:35:06 PM9/13/05
to django...@googlegroups.com
Hey Andreas --

I actually ran into this same problem myself today. It appears to be
a bug in the admin changelist view; until I get a chance to actually
track it down you can work around it by adding a "ordering" option to
your META.admin::

class FirstChild(meta.Model):
...
class META:
admin = meta.Admin(ordering=('myvar',))
...

Jacob

Andreas

unread,
Sep 13, 2005, 9:42:00 PM9/13/05
to Django users
Hi Jacob,

thanks, it's good to know that I'm not going to find the bug in my
code.

While trying different things, I too found out that adding the
"ordering"-option made the listview work again. The first thing I tried
was adding ordering = ['theparent']:

class FirstChild(meta.Model):
...
class META:

admin = meta.Admin(ordering = ['theparent'])

The resulting Admin view was a table in which every entry appeared
three times instead of once. Is this a related bug, something different
or is "ordering" by foreign keys not supported?

Andreas

Reply all
Reply to author
Forward
0 new messages