Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#6127 closed (duplicate)

Admin interface bugs when a ForeignKey is in list_display

Reported by: gkelly Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:


As seen at

It appears the admin interface isn't properly translating something like '-id' into 'id' DESC when producing SQL (for postgres at least).

Django r6893.

File "/usr/local/lib/python2.4/site-packages/django/template/" in render_node
  822.             result = node.render(context)
File "/usr/local/lib/python2.4/site-packages/django/template/" in render
  982.                     dict = func(*args)
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/templatetags/" in result_list
  209.     return {'cl': cl,
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/templatetags/" in results
  205.     for res in cl.result_list:
File "/usr/local/lib/python2.4/site-packages/django/db/models/" in __iter__
  114.         return iter(self._get_data())
File "/usr/local/lib/python2.4/site-packages/django/db/models/" in _get_data
  483.             self._result_cache = list(self.iterator())
File "/usr/local/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/local/lib/python2.4/site-packages/django/db/backends/" in execute
  18.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.4/site-packages/django/db/backends/postgresql/" 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)

admin-ordering.patch (699 bytes) - added by gkelly 9 years ago.
Admin foreignkey ordering fix

Download all attachments as: .zip

Change History (4)

Changed 9 years ago by gkelly

Admin foreignkey ordering fix

comment:1 Changed 9 years ago by gkelly

  • Cc gkelly@… added
  • Has patch set
  • Keywords foreignkey list_display added
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement set
  • Summary changed from Admin ordering producing invalid SQL to 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:2 Changed 9 years ago by ubernostrum

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

Duplicate of #2895.

comment:3 Changed 9 years ago by gkelly

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.

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