Package: django-admin-autocomplete-filter
Django Admin Autocomplete FilterA simple Django app to render list filters in django admin using an autocomplete widget. This app is heavily inspired bydal-admin-filters.Overview:Django comes preshipped with an admin panel which is a great utility to create quick CRUD's.
Version 2.0 came with a much neededautocomplete_fieldsproperty which uses a select2 widget to load the options asynchronously.  We leverage this indjango-admin-list-filter.Requirements:Requires Django version >= 2.0Features:Custom search view/endpoint (more details)list_filterFilter Factory support (more details)Custom widget text (more details)Support forGrappelliInstallation:You can install it via pip.  To get the latest version clone this repo.pipinstalldjango-admin-autocomplete-filterAddadmin_auto_filtersto yourINSTALLED_APPSinside settings.py of your project.Usage:Let's say we have following models:fromdjango.dbimportmodelsclassArtist(models.Model):name=models.CharField(max_length=128)classAlbum(models.Model):name=models.CharField(max_length=64)artist=models.ForeignKey(Artist,on_delete=models.CASCADE)cover=models.CharField(max_length=256,null=True,default=None)And you would like to filter results inAlbumAdminon the basis ofartist.  You need to definesearch fieldsinArtistand then define filter like this:fromdjango.contribimportadminfromadmin_auto_filters.filtersimportAutocompleteFilterclassArtistFilter(AutocompleteFilter):title='Artist'# display titlefield_name='artist'# name of the foreign key fieldclassArtistAdmin(admin.ModelAdmin):search_fields=['name']# this is required for django's autocomplete functionality# ...classAlbumAdmin(admin.ModelAdmin):list_filter=[ArtistFilter]# ...After following these steps you may see the filter as:Functionality to provide a custom view for search:You can also register your custom view instead of using Django admin'ssearch_resultsto control the results in the autocomplete. For this you will need to create your custom view and register the URL in your admin class as shown below:In yourviews.py:fromadmin_auto_filters.viewsimportAutocompleteJsonViewclassCustomSearchView(AutocompleteJsonView):defget_queryset(self):"""your custom logic goes here."""queryset=super().get_queryset()queryset=queryset.order_by('name')returnquerysetAfter this, register this view in your admin class:fromdjango.contribimportadminfromdjango.urlsimportpathclassAlbumAdmin(admin.ModelAdmin):list_filter=[ArtistFilter]defget_urls(self):urls=super().get_urls()custom_urls=[path('custom_search/',self.admin_site.admin_view(CustomSearchView.as_view(model_admin=self)),name='custom_search'),]returncustom_urls+urlsFinally, just tell the filter class to use this new view:fromdjango.shortcutsimportreversefromadmin_auto_filters.filtersimportAutocompleteFilterclassArtistFilter(AutocompleteFilter):title='Artist'field_name='artist'defget_autocomplete_url(self,request,model_admin):returnreverse('admin:custom_search')Shortcut for creating filters:It's also possible to use theAutocompleteFilterFactoryshortcut to create
filters on the fly, as shown below. Nested relations are supported too, with
no need to specify the model.fromdjango.contribimportadminfromadmin_auto_filters.filtersimportAutocompleteFilterFactoryclassAlbumAdmin(admin.ModelAdmin):list_filter=[AutocompleteFilterFactory('Artist','artist','admin:custom_search',True)]defget_urls(self):"""As above..."""Customizing widget textYou can customize the text displayed in the filter widget, to use something
other thanstr(obj). This needs to be configured for both the dropdown
endpoint and the widget itself.In yourviews.py, overridedisplay_text:fromadmin_auto_filters.viewsimportAutocompleteJsonViewclassCustomSearchView(AutocompleteJsonView):@staticmethoddefdisplay_text(obj):returnobj.my_str_method()defget_queryset(self):"""As above..."""Then use either of two options to customize the text.Option one is to specify the form_field in an AutocompleteFilter in youradmin.py:fromdjangoimportformsfromdjango.contribimportadminfromdjango.shortcutsimportreversefromadmin_auto_filters.filtersimportAutocompleteFilterclassFoodChoiceField(forms.ModelChoiceField):deflabel_from_instance(self,obj):returnobj.my_str_method()classArtistFilter(AutocompleteFilter):title='Artist'field_name='artist'form_field=FoodChoiceFielddefget_autocomplete_url(self,request,model_admin):returnreverse('admin:custom_search')classAlbumAdmin(admin.ModelAdmin):list_filter=[ArtistFilter]defget_urls(self):"""As above..."""Option two is to use an AutocompleteFilterFactory in youradmin.pyadd alabel_byargument:fromdjango.contribimportadminfromadmin_auto_filters.filtersimportAutocompleteFilterFactoryclassAlbumAdmin(admin.ModelAdmin):list_filter=[AutocompleteFilterFactory('Artist','artist','admin:custom_search',True,label_by='my_str_method')]defget_urls(self):"""As above..."""Contributing:This project is a combined effort of a lot of selfless developers who try to make things easier. Your contribution is most welcome.Please make a pull-request to the branchpre_release, make sure your branch does not have any conflicts, and clearly mention the problems or improvements your PR is addressing.License:Django Admin Autocomplete Filter is an Open Source project licensed under the terms of the GNU GENERAL PUBLIC LICENSE.