#6127 closed (duplicate)
Admin interface bugs when a ForeignKey is in list_display
| Reported by: | gkelly | Owned by: | nobody | 
|---|---|---|---|
| Component: | contrib.admin | Version: | dev | 
| Severity: | Keywords: | admin invalid SQL ordering foreignkey list_display | |
| Cc: | gkelly@… | Triage Stage: | Unreviewed | 
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | yes | Patch needs improvement: | yes | 
| Easy pickings: | no | UI/UX: | no | 
Description
As seen at http://dpaste.com/26820/
It appears the admin interface isn't properly translating something like '-id' into 'id' DESC when producing SQL (for postgres at least).
Django r6893. 
Traceback:
File "/usr/local/lib/python2.4/site-packages/django/template/__init__.py" in render_node
  822.             result = node.render(context)
File "/usr/local/lib/python2.4/site-packages/django/template/__init__.py" in render
  982.                     dict = func(*args)
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  209.     return {'cl': cl,
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  205.     for res in cl.result_list:
File "/usr/local/lib/python2.4/site-packages/django/db/models/query.py" in __iter__
  114.         return iter(self._get_data())
File "/usr/local/lib/python2.4/site-packages/django/db/models/query.py" in _get_data
  483.             self._result_cache = list(self.iterator())
File "/usr/local/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/local/lib/python2.4/site-packages/django/db/backends/util.py" in execute
  18.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.4/site-packages/django/db/backends/postgresql/base.py" in execute
  47.         return self.cursor.execute(smart_str(sql, self.charset), self.format_params(params))
Exception Type: ProgrammingError at /wxcoder/admin/observations/observation/
Exception Value: ERROR:  column sites_station.-id does not exist at character 1558
SELECT "observations_observation"."id","observations_observation"."station_id","observations_observation"."date","observations_observation"."entered","observations_observation"."type","observations_observation"."correction","observations_observation"."confirmed","observations_observation"."coded","observations_observation"."xmitted","observations_observation"."comment","observations_observation"."user_id","sites_station"."id","sites_station"."siteID","sites_station"."siteNumber","sites_station"."name","sites_station"."county","sites_station"."state_id","sites_station"."type","sites_station"."obType","sites_station"."obSubType","sites_station"."obTime","sites_station"."obTZ_id","sites_station"."obFlags","sites_station"."xmitCoding","sites_station"."xmitID","sites_station"."reports","sites_station"."lon","sites_station"."lat","sites_station"."elev","sites_station"."wfo_id","sites_station"."ucan_station_id","common_state"."id","common_state"."name","common_state"."abbr","common_state"."fips","common_state"."zipRange","auth_user"."id","auth_user"."username","auth_user"."first_name","auth_user"."last_name","auth_user"."email","auth_user"."password","auth_user"."is_staff","auth_user"."is_active","auth_user"."is_superuser","auth_user"."last_login","auth_user"."date_joined" FROM "observations_observation" , "sites_station", "common_state", "auth_user" WHERE "observations_observation"."station_id" = "sites_station"."id" AND "sites_station"."state_id" = "common_state"."id" AND "observations_observation"."user_id" = "auth_user"."id" ORDER BY "sites_station"."-id" ASC LIMIT 100 
      Attachments (1)
Change History (4)
by , 18 years ago
| Attachment: | admin-ordering.patch added | 
|---|
comment:1 by , 18 years ago
| Cc: | added | 
|---|---|
| Has patch: | set | 
| Keywords: | foreignkey list_display added | 
| Needs tests: | set | 
| Patch needs improvement: | set | 
| Summary: | Admin ordering producing invalid SQL → Admin interface bugs when a ForeignKey is in list_display | 
I added a patch to fix the admin interface in my case, but I fear there may be a larger issue elsewhere. Has the current admin interface been abandoned for the newforms-admin?
Try putting a ForeignKey field in class Admin.list_display and the admin interface does weird things (like the incorrect ordering SQL), as well as failing to display more than the first page of records. Can someone else duplicate this? I can try to make up a small example.
comment:3 by , 18 years ago
For future reference, I just noticed that my model to which the ForeignKey relates has
class Meta:
    ordering = ['-id',]
which is probably where the invalid SQL was coming from. The problem goes away if Meta.ordering is removed.
Admin foreignkey ordering fix