Django autocomplete Light in list filters for admin
Asked Answered
E

2

9

I have successfully setup the Autocomplete Registry and have my django admin forms where if you go to the form, the auto completes works. I would like to be able to extend the autocompletes to work on the list_filter view as well. So when you are looking at the view generated by Admin.py -- that the list_filter inputs that are generated would also use the autocomplete jquery + service URL.

I didn't see anything listed in the documentation, anyone have any pointers?

Etruscan answered 14/2, 2016 at 6:5 Comment(0)
F
5

If you are using Django version greater then 2.0, you can try using the built-in autocomplete fields for this purpose.

By default, the admin uses a select-box interface () for those fields. Sometimes you don’t want to incur the overhead of selecting all the related instances to display in the dropdown.

The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously

There is a simple app which does this:

To install use: pip install django-admin-autocomplete-filter

Then add admin_auto_filters to your INSTALLED_APPS inside settings.py of your project.

Let's say we have following models:

class Artist(models.Model):
    name = models.CharField(max_length=128)

class Album(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 in Album Admin on the basis of artist, then you can define search fields in Artist and then define filter as:

from admin_auto_filters.filters import AutocompleteFilter

class ArtistFilter(AutocompleteFilter):
    title = 'Artist' # display title
    field_name = 'artist' # name of the foreign key field

class ArtistAdmin(admin.ModelAdmin):
    search_fields = ['name'] # this is required for django's autocomplete functionality
    ...

class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]
    
    '''
       defining this class is required for AutocompleteFilter
       it's a bug and I am working on it.
    '''
    class Media:
        pass

After following these steps you may see the filter as:

autocomplete filter

search as you type

Fluted answered 17/11, 2018 at 5:6 Comment(4)
Only works for ForeingKeys not for CharFieldsOecd
Attention: django-admin-autocomplete-filter uses the GPL license, which makes it unusable for a lot of use cases.Nicholnichola
django-admin-autocomplete-filter is a nice implementation but limited by the GPL and the support for ForeignKeys only. django-admin-list-filter-dropdown does a good job of more support but isn't as robust or as nice looking as django-admin-autocomplete-filter.Cubeb
@vishal django-admin-list-filter-dropdown is not a autocomplete filterProperty
S
2

You should define your own admin filter that inherits from django.contrib.admin.SimpleListFilter. Then should provide your own HTML template for this filter which will use one of django-autocomplete-light widgets. As a parameter for widget you should define required autocomplete URL. And do not forget to include proper JS and CSS for it.

All of this is done in special app for this: dal-admin-filters

Storfer answered 7/6, 2017 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.