Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#168 closed defect (fixed)

history() in django/views/admin/main.py makes assumptions about the object's key name

Reported by: clintecker@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: major Keywords: history
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Normally the ID of an object is "id", except in cases like mine where I'd redefined the key to be something like list:

"meta.AutoField('item_id','Item ID',primary_key=True,unique=True),"

When pulling up the history of a record in the administrative interface, the function history() in django/views/admin/main.py is called and an assumption is made on line 1080:

"obj = mod.get_object(id__exact=object_id)"

In my case, I believe that it should be (it cannot be hardcoded like this, obviously this needs to be dynamic):

"obj = mod.get_object(item_id__exact=object_id)"

Here's the exact text of the traceback I get:

There's been an error:

Traceback (most recent call last):

  File "/usr/local/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 190, in get_response
    return callback(request, **param_dict)

  File "/usr/local/lib/python2.4/site-packages/django/views/admin/main.py", line 1080, in history
    obj = mod.get_object(id__exact=object_id)

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 87, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 1035, in function_get_object
    obj_list = function_get_list(opts, klass, **kwargs)

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 1057, in function_get_list
    select, sql, params = function_get_sql_clause(opts, **kwargs)

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 1235, in function_get_sql_clause
    tables2, join_where2, where2, params2, _ = _parse_lookup(kwargs.items(), opts)

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 1224, in _parse_lookup
    _throw_bad_kwarg_error(kwarg)

  File "/usr/local/lib/python2.4/site-packages/django/core/meta.py", line 1126, in _throw_bad_kwarg_error
    raise TypeError, "got unexpected keyword argument '%s'" % kwarg

TypeError: got unexpected keyword argument 'id__exact' 

Change History (5)

comment:1 by clintecker@…, 19 years ago

Attaching a fix for this

comment:2 by anonymous, 19 years ago

--- main.py     Fri Jul 22 23:47:51 2005
+++ main-old.py Fri Jul 22 23:47:34 2005
@@ -1077,7 +1077,7 @@
         order_by=("action_time",), select_related=True)
     # If no history was found, see whether this object even exists.
     try:
-        obj = mod.get_object(**{'%s__exact' % opts.pk.name: object_id})
+        obj = mod.get_object(id__exact=object_id)
     except ObjectDoesNotExist:
         raise Http404
     t = template_loader.get_template('admin_object_history')

comment:3 by clintecker@…, 19 years ago

Sorry that was backwards:

--- main-old.py Fri Jul 22 23:47:34 2005
+++ main.py     Fri Jul 22 23:47:51 2005
@@ -1077,7 +1077,7 @@
         order_by=("action_time",), select_related=True)
     # If no history was found, see whether this object even exists.
     try:
-        obj = mod.get_object(id__exact=object_id)
+        obj = mod.get_object(**{'%s__exact' % opts.pk.name: object_id})
     except ObjectDoesNotExist:
         raise Http404
     t = template_loader.get_template('admin_object_history')

comment:4 by rmunn@…, 19 years ago

Alternately, if the patch in #163 for providing a "pk=(value)" syntax is accepted, the following patch should also solve the bug:

Index: django/views/admin/main.py
===================================================================
--- django/views/admin/main.py  (revision 300)
+++ django/views/admin/main.py  (working copy)
@@ -1077,7 +1077,7 @@
         order_by=("action_time",), select_related=True)
     # If no history was found, see whether this object even exists.
     try:
-        obj = mod.get_object(id__exact=object_id)
+        obj = mod.get_object(pk=object_id)
     except ObjectDoesNotExist:
         raise Http404
     t = template_loader.get_template('admin_object_history')

comment:5 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

(In [302]) Fixed #168 -- admin object history was assuming primary key was named 'id'. Thanks, clintecker@…

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