Django

Code

Ticket #6127 (closed: duplicate)

Opened 1 year ago

Last modified 1 year ago

Admin interface bugs when a ForeignKey is in list_display

Reported by: gkelly Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: SVN Keywords: admin invalid SQL ordering foreignkey list_display
Cc: gkelly@gmail.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

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

admin-ordering.patch (0.7 kB) - added by gkelly on 12/05/07 14:17:48.
Admin foreignkey ordering fix

Change History

12/05/07 14:17:48 changed by gkelly

  • attachment admin-ordering.patch added.

Admin foreignkey ordering fix

12/05/07 14:21:25 changed by gkelly

  • cc set to gkelly@gmail.com.
  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • summary changed from Admin ordering producing invalid SQL to Admin interface bugs when a ForeignKey is in list_display.
  • keywords changed from admin invalid SQL ordering to admin invalid SQL ordering foreignkey list_display.
  • needs_docs changed.
  • has_patch set to 1.

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.

12/05/07 14:26:18 changed by ubernostrum

  • status changed from new to closed.
  • resolution set to duplicate.

Duplicate of #2895.

12/05/07 14:47:54 changed 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.


Add/Change #6127 (Admin interface bugs when a ForeignKey is in list_display)




Change Properties
Action