#7098 closed (fixed)
FieldError Invalid order_by arguments in Admin pages when sorting on a ForeignKey
| Reported by: | trbs | Owned by: | nobody |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Keywords: | order_by | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
In Trunk with the merged queryset-refactor branch, the following problem happens when trying to sort on a foreign key in the admin menu.
As an example i've used the contrib.comments and sorted on the 'user' column. (which is a foreign key to auth user model)
I've attached a patch (which seems) to fix this on this ticket.
Comments are appreciated :)
Error Message / Stack trace:
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/comments/comment/
Django Version: 0.97-pre-SVN-unknown
Python Version: 2.5.2
Installed Applications:
['django.contrib.sites',
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.comments',
'django.contrib.contenttypes',
'django.contrib.humanize',
'django.contrib.redirects',
'django.contrib.sessions',
'django.contrib.sitemaps',
'django.contrib.markup',
'extensions',
'comment_utils',
'template_utils',
'django.contrib.flatpages',
'django.contrib.databrowse']
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.CacheMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')
Traceback:
File "/usr/src/django/trunk/django/core/handlers/base.py" in get_response
82. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/src/django/trunk/django/contrib/admin/views/decorators.py" in _checklogin
62. return view_func(request, *args, **kwargs)
File "/usr/src/django/trunk/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/src/django/trunk/django/contrib/admin/views/main.py" in change_list
772. cl = ChangeList(request, model)
File "/usr/src/django/trunk/django/contrib/admin/views/main.py" in __init__
581. self.query_set = self.get_query_set()
File "/usr/src/django/trunk/django/django/contrib/admin/views/main.py" in get_query_set
736. qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
File "/usr/src/django/trunk/django/db/models/query.py" in order_by
427. obj.query.add_ordering(*field_names)
File "/usr/src/django/trunk/django/db/models/sql/query.py" in add_ordering
1293. raise FieldError('Invalid order_by arguments: %s' % errors)
Exception Type: FieldError at /admin/comments/comment/
Exception Value: Invalid order_by arguments: ['auth_user.username']
Attachments (1)
Change History (6)
by , 18 years ago
| Attachment: | 7484-contrib.admin.views.main-fix-order-by.patch added |
|---|
comment:1 by , 18 years ago
comment:2 by , 18 years ago
I'll commit your patch from #3002 in preference to this one for the admin side of things.
However, the actual error is caused more by a bug in core code. It wasn't intended to prohibited the use of table_name.field style ordering, because I don't want to break existing code. I'll commit a patch to fix that problem which will close this ticket.
From a little discussion which followed on irc, this ticket seems to be a (partial?) duplicate of #3002 and #7099 (later for newforms-admin branch):
This patch is only mend to fix the breakage of Admin on sorting on ForeignKey's in trunk. Not to improve of fix the ordering functionality at large.
So my hope is that either for the short term, this (or alike) patch is applied to fix the breakage, or if the other ticket is ready for checking this ticket is marked as duplicate and the other patch is committed.