Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#423 closed defect (fixed)

Admin filters broken with [549]

Reported by: jkocherhans@… Owned by: Adrian Holovaty
Component: contrib.admin Version:
Severity: normal 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 changed my models over to the new syntax and svn up'd django this morning. Everything seems to work fine except for filters in the admin interface. I have a ForeignKey realtion called 'site'... the admin interface generates a url like:

http://127.0.0.1:8000/childabuse/data/patients/?site__exact=4

Which results in the traceback below. If I manually type in the url:

http://127.0.0.1:8000/childabuse/data/patients/?site__id__exact=4

It works as expected. I'm not sure if this is a problem with the admin interface, or the db api. According to the new db api docs, it looks like a problem with the admin interface though.

Traceback (most recent call last):

  File "/usr/local/lib/python2.4/site-packages/django/core/handlers/base.py", line 64, in get_response
    response = callback(request, **param_dict)

  File "/usr/local/lib/python2.4/site-packages/django/views/admin/main.py", line 177, in change_list
    result_count = p.hits

  File "/usr/local/lib/python2.4/site-packages/django/core/paginator.py", line 67, in _get_hits
    self._hits = getattr(self.module, self.count_method)(**order_args)

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

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

  File "/usr/local/lib/python2.4/site-packages/django/core/meta/__init__.py", line 1299, 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/__init__.py", line 1228, in _parse_lookup
    _throw_bad_kwarg_error(kwarg)

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

TypeError: got unexpected keyword argument 'site__exact'

Change History (3)

comment:1 by jkocherhans@…, 19 years ago

I haven't tested the code below, but it should show my point.

class Foo(meta.Model):
    name = meta.CharField(maxlength=255)

class Bar(meta.Model):
    name = meta.CharField(maxlength=255)
    foo = ForeignKey(Foo)
    class META:
        admin = meta.Admin(
            list_filter=('foo',),
        )

The link for each filter item is like:

http://127.0.0.1:8000/admin/appname/bars/?foo__exact=1

http://127.0.0.1:8000/admin/appname/bars/?foo__exact=2

etc, but the page for this links don't work. (See the taceback above.) These work however:

http://127.0.0.1:8000/admin/appname/bars/?foo__id__exact=1

http://127.0.0.1:8000/admin/appname/bars/?foo__id__exact=2

Despite reading the docs, I'm not sure what the expected behavior is. If the second set set of links should work, here's a patch to fix the admin interface. If the first set of links should work, then I can't figure out how to fix it at first glance. It seems to me, that with the new model syntax, both should work. Hopefully this is a little clearer than my initial report.

Index: django/views/admin/main.py
===================================================================
--- django/views/admin/main.py  (revision 558)
+++ django/views/admin/main.py  (working copy)
@@ -199,7 +199,7 @@
                     lookup_kwarg = '%s__id__exact' % f.name
                     lookup_title = f.rel.to.verbose_name
                 else:
-                    lookup_kwarg = '%s__exact' % f.name
+                    lookup_kwarg = '%s__id__exact' % f.name
                     lookup_title = f.verbose_name
                 lookup_val = request.GET.get(lookup_kwarg, None)
                 lookup_choices = f.rel.to.get_model_module().get_list()

comment:2 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:3 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: assignedclosed

Fixed in [569].

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