palewire / django-calaccess-raw-data

A Django app to download, extract and load campaign finance and lobbying activity data from the California Secretary of State's CAL-ACCESS database
http://django-calaccess.californiacivicdata.org/
MIT License
64 stars 143 forks source link

Searching admin in lobbyingchglogcd results in "Related Field got invalid lookup: icontains" #1498

Closed hcharley closed 7 years ago

hcharley commented 7 years ago

Using package from pypi: django-calaccess-raw-data==1.4.6

This is my query URL: http://0.0.0.0:8000/admin/calaccess_raw/lobbyingchglogcd/?q=test

This is my traceback:

Environment:

Request Method: GET
Request URL: http://0.0.0.0:8000/admin/calaccess_raw/lobbyingchglogcd/?q=test

Django Version: 1.10.4
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admindocs',
 'calaccess_raw',
 'my_project']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  544.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  211.             return view(request, *args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1543.                 self.list_max_show_all, self.list_editable, self,

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in __init__
  78.         self.queryset = self.get_queryset(request)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in get_queryset
  346.         qs, search_use_distinct = self.model_admin.get_search_results(request, qs, self.query)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_search_results
  905.                 queryset = queryset.filter(reduce(operator.or_, or_queries))

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/query.py" in filter
  796.         return self._filter_or_exclude(False, *args, **kwargs)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  814.             clone.query.add_q(Q(*args, **kwargs))

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
  1227.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1247.                     current_negated, allow_joins, split_subq)

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1253.                     allow_joins=allow_joins, split_subq=split_subq,

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1178.             lookup_class = field.get_lookup(lookups[0])

File "/Users/cbodkin/.virtualenvs/my_project/lib/python2.7/site-packages/django/db/models/fields/related.py" in get_lookup
  694.             raise TypeError('Related Field got invalid lookup: %s' % lookup_name)

Exception Type: TypeError at /admin/calaccess_raw/lobbyingchglogcd/
Exception Value: Related Field got invalid lookup: icontains
gordonje commented 7 years ago

Pretty sure this is happening on admin of a model with a ForeignKeyField, so it's a problem on RawDataFile, FilerToFilerTypeCd and FilerTypePeriodsCd as well.

All of our admins inherit from BaseAdmin, where we've defined a method to make all fields on a given model searchable. For now, I am going to exclude these ForeignKeyFields from being searchable.

If we need to customize any admins to allow searching on field of a related model, we can just override the .get_search_fields() to add those specific fields. Django docs says it is possible for related lookups.

Thanks for the heads up!