Django-grappelli: Autocomplete does not work with Django 3.2

Created on 2 May 2021  ·  15Comments  ·  Source: sehmaschine/django-grappelli

VERSIONS:
Grappelli 2.15.1, Django 3.2
STATICFILES: WARNINGS,
Details:
I see a lot of errors with text like this

Found another file with the destination path 'admin/js/autocomplete.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

JAVASCRIPTS: ERROR
Failed to load resource: the server responded with a status of 403 (Forbidden)
CUSTOMIZATIONS: NONE

I upgraded django from 2.2 to 3.2 and it broke the autocomplete on related fields it just always says 403 and gives the following error:

Forbidden (Permission denied): /admin/autocomplete/
Traceback (most recent call last):
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/utils/datastructures.py", line 76, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'app_label'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/views/autocomplete.py", line 61, in process_request
    app_label = request.GET['app_label']
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'app_label'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 250, in wrapper
    return self.admin_view(view, cacheable)(*args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 417, in autocomplete_view
    return AutocompleteJsonView.as_view(admin_site=self)(request)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/views/autocomplete.py", line 20, in get
    self.term, self.model_admin, self.source_field, to_field_name = self.process_request(request)
  File "/home/alex/Project/env/lib/python3.8/site-packages/django/contrib/admin/views/autocomplete.py", line 65, in process_request
    raise PermissionDenied from e
django.core.exceptions.PermissionDenied
[02/May/2021 20:11:33] "GET /admin/autocomplete/ HTTP/1.1" 403 135

This ticket seems to be related to the issue
https://code.djangoproject.com/ticket/32659?cversion=2&cnum_hist=2

Downgrade to 3.1 fixes the issue.

Most helpful comment

thanks, I see. I'll check that.

All 15 comments

not sure I can help here. the warning looks "ok" (since we overwrite django files), you can ignore that. if the JS is not being loaded properly, there will be errors of course. I'm not sure why you get a 403, but it's definitely not related to grappelli.

Django 3.2, Grappelli 2.15.1

It looks like a problem is the fact that there are two jQuery versions being loaded, 3.3.1 from Grappelli and 3.5.1 from Django 3.2.

https://code.djangoproject.com/ticket/32659

It's definitely a Grappelli problem at least partially unless Grappelli does not intend to support ModelAdmin.autocomplete_fields. And while on usual fields it's possible to replace with raw_id_fields, the https://github.com/farhan0581/django-admin-autocomplete-filter may never work without it. The simple solution may be updating jQuery in Grappelli, not sure if that will work.

Again, it does work if I remove grappelli from installed apps and do collectstatic and it stops working immediately after I add it back and perform collectstatic again.

You get 403 because with Grappelli there are no app_label, model_name and field_name in GET parameters, only term.

And they are not there because /static/admin/js/autocomplete.js is outdated. Please sync it with upstream.

Workaround for anyone else experiencing this: create an empty Django app with the upstream version of autocomplete.js in static/admin/js and put it before grappelli in the INSTALLED_APPS. Edit: I've also included override for emptying jquery-migrate and update for grappelli/jquery to 3.5.1. Let's see what will break.

Please comment if you discover more outdated JS files that have to be synced with upstream. I guess it's likely there are more of them.

just to clarify. which "autocompletes" do you refer to? the django autocompletes or the grappelli autocompletes?

Django autocompletes, the ones used in https://github.com/farhan0581/django-admin-autocomplete-filter Sorry, that was ambiguous, yes.

thanks, I see. I'll check that.

Thank you! Just noticed that admin/filter_listing.html doesn't support these Django autocompletes, shows only All option without the select element. Should I create a separate issue for that?

not sure I can follow. filter with autocompletes? does django support that? if so, I haven´t been aware. but yes, please open a separate ticket ...

Not by default, only with the aforementioned django-admin-autocomplete-filter. So it's more like support for that module rather than a bug. I'll try to do it with a custom change_list_filter_template first.

Any updates on this?

We'll look into this as soon as we have some spare time. If you're in a hurry, a PR might help.

Any direction for creating a PR? Anything I should know?

Just use Django3.1

Django 3.2 still does not work.

no fix 3.2.4

Was this page helpful?
0 / 5 - 0 ratings