Opened 7 years ago

Closed 7 years ago

#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: master
Severity: Keywords: order_by
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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:


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:
Installed Middleware:

File "/usr/src/django/trunk/django/core/handlers/" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/src/django/trunk/django/contrib/admin/views/" in _checklogin
  62.             return view_func(request, *args, **kwargs)
File "/usr/src/django/trunk/django/views/decorators/" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/usr/src/django/trunk/django/contrib/admin/views/" in change_list
  772.         cl = ChangeList(request, model)
File "/usr/src/django/trunk/django/contrib/admin/views/" in __init__
  581.         self.query_set = self.get_query_set()
File "/usr/src/django/trunk/django/django/contrib/admin/views/" 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/" in order_by
  427.         obj.query.add_ordering(*field_names)
File "/usr/src/django/trunk/django/db/models/sql/" 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)

7484-contrib.admin.views.main-fix-order-by.patch (692 bytes) - added by trbs 7 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 Changed 7 years ago by trbs

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

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.

comment:2 Changed 7 years ago by mtredinnick

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.

comment:3 Changed 7 years ago by mtredinnick

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

Fixed in [7490].

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