farhan0581 / django-admin-autocomplete-filter

A simple Django app to render list filters in django admin using autocomplete widget.
GNU General Public License v3.0
351 stars 75 forks source link

`views.AutocompleteJsonView` fix for Django>=3.2 #79

Open millioner opened 2 years ago

millioner commented 2 years ago

Custom autocomplete view fails with Django>=3.2

This code

    def get_urls(self) -> List:
        from admin_auto_filters.views import AutocompleteJsonView
        custom_urls = [
            path(
                'autocomplete/',
                self.admin_site.admin_view(
                    AutocompleteJsonView.as_view(model_admin=self),
                ),
                name='autocomplete',
            ),
        ]
        return custom_urls + super().get_urls()

Gives this error:

TypeError: AutocompleteJsonView() received an invalid keyword 'model_admin'. as_view only accepts arguments that are already attributes of the class.

The reason: django.contrib.admin.views.AutocompleteJsonView class creates the model_admin property only during a requst processing.

Here is the commit in the Django repo - https://github.com/django/django/commit/3071660acfbdf4b5c59457c8e9dc345d5e8894c5

bn-rv commented 2 years ago
self.term, self.model_admin, self.source_field, _ = self.process_request(request)
File "...django/contrib/admin/views/autocomplete.py", line 82, in process_request
model_admin = self.admin_site._registry[remote_model]
AttributeError: 'NoneType' object has no attribute '_registry'
millioner commented 2 years ago

Yep, my bad. Docs are bit updated now.

shaig-planetly commented 2 years ago

@millioner I tested but this doesn't fix the issues for django>=3.2. Thanks for the PR btw!