Opened 17 years ago

Closed 17 years ago

#6649 closed (invalid)

Can't order by a ForeignKey relation

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

Description

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:

Environment:

Request Method: GET
Request URL: http://together.cs.washington.edu:8082/admin/main/pubaward/
Django Version: 0.97-pre-SVN-7149
Python Version: 2.4.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'dubweb.main',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py" in _checklogin
  56.             return view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  39.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in change_list
  776.         cl = ChangeList(request, model)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in __init__
  582.         self.get_results(request)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in get_results
  640.             result_list = list(self.query_set)
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in __iter__
  114.         return iter(self._get_data())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in _get_data
  483.             self._result_cache = list(self.iterator())
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" 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/util.py" in execute
  18.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.4/site-packages/django/db/backends/sqlite3/base.py" 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 by Malcolm Tredinnick, 17 years ago

Resolution: invalid
Status: newclosed

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