Opened 16 years ago

Closed 16 years ago

Last modified 3 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: 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


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 16 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by trbs, 16 years ago

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 by Malcolm Tredinnick, 16 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.

comment:3 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

Fixed in [7490].

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Easy pickings: unset
UI/UX: unset

In 98ea4f0f:

Refs #7098 -- Deprecated passing raw column aliases to order_by().

Now that order_by() has expression support passing RawSQL() can achieve
the same result.

This was also already supported through QuerySet.extra(order_by) for
years but this API is more or less deprecated at this point.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 06eec31:

Refs #7098 -- Removed support for passing raw column aliases to order_by().

Per deprecation timeline.

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