Opened 8 years ago

Closed 8 years ago

#6649 closed (invalid)

Can't order by a ForeignKey relation

Reported by: toomim Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have a model Venue that is sorted by "-pub_date":

class Venue(models.Model):
    pub_date = models.DateField('Published date', help_text="Only the year and month will be used")
    class Admin: pass
    class Meta:
        ordering = ("-pub_date",)

And a model PubAward that foreignkeys Venue, and sorts by Venue:

class PubAward(models.Model):
    venue = models.ForeignKey(Venue, blank=True)
    class Admin: pass
    class Meta:
        ordering = ("venue",)

But when I open an admin page on PubAward, it tries to list all PubAwards and gives me the following error:

no such column: main_venue.-pub_date

Here's the full stack trace:


Request Method: GET
Request URL:
Django Version: 0.97-pre-SVN-7149
Python Version: 2.4.4
Installed Applications:
Installed Middleware:

File "/usr/lib/python2.4/site-packages/django/core/handlers/" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in _checklogin
  56.             return view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/views/decorators/" in _wrapped_view_func
  39.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in change_list
  776.         cl = ChangeList(request, model)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in __init__
  582.         self.get_results(request)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/" in get_results
  640.             result_list = list(self.query_set)
File "/usr/lib/python2.4/site-packages/django/db/models/" in __iter__
  114.         return iter(self._get_data())
File "/usr/lib/python2.4/site-packages/django/db/models/" in _get_data
  483.             self._result_cache = list(self.iterator())
File "/usr/lib/python2.4/site-packages/django/db/models/" in iterator
  189.         cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
File "/usr/lib/python2.4/site-packages/django/db/backends/" in execute
  18.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.4/site-packages/django/db/backends/sqlite3/" in execute
  133.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/main/pubaward/
Exception Value: no such column: main_venue.-pub_date

This also occurs without the hyphen in "-pub_date".

Change History (1)

comment:1 Changed 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Ordering by related fields doesn't work like that in trunk. Please read the documentation for order_by carefully. Also search the django-users archives, since this issue comes up frequently (including mention that it's being worked on to make it more natural).

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