Skip to main content

Render admin list filters with Django's autocomplete widget

Project description

PyPI version Tests Ruff

Django Admin Autocomplete Filters

Maintained continuation of the original project by Farhan Khan: https://github.com/farhan0581/django-admin-autocomplete-filter This fork modernizes packaging (PEP 621), adds CI, and supports Django 4.2–5.2 and Python 3.10+. A simple Django app to render list filters in django admin using an autocomplete widget. This app is heavily inspired by dal-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 needed autocomplete_fields property which uses a select2 widget to load the options asynchronously. We leverage this in django-admin-list-filter.

Requirements:

  • Django >= 4.2
  • Python >= 3.10

Supported Versions

  • Python: 3.10, 3.11, 3.12
  • Django: 4.2, 5.0, 5.1, 5.2

See the CI badge for the full matrix.

Features:

Installation:

You can install it via pip. To get the latest version clone this repo.

pip install django-admin-autocomplete-filters

Add admin_auto_filters to your INSTALLED_APPS inside settings.py of your project.

Usage:

Let's say we have following models:

from django.db import 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 AlbumAdmin on the basis of artist. You need to define search fields in Artist and then define filter like this:

from django.contrib import admin
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]
    # ...

After following these steps you may see the filter as:

Release Process

  • Bump admin_auto_filters.__version__ and update CHANGELOG.md.
  • Ensure CI is green on main.
  • Create a tag vX.Y.Z and push; the GitHub Actions workflow will build and publish to PyPI via trusted publishing.

Contributing

See CONTRIBUTING.md for local setup, linting, typing, tests, and pre-commit hooks.

Functionality to provide a custom view for search:

You can also register your custom view instead of using Django admin's search_results to 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 your views.py:

from admin_auto_filters.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):
    def get_queryset(self):
        """
           your custom logic goes here.
        """
        queryset = super().get_queryset()
        queryset = queryset.order_by('name')
        return queryset

After this, register this view in your admin class:

from django.contrib import admin
from django.urls import path


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]

    def get_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'),
        ]
        return custom_urls + urls

Finally, just tell the filter class to use this new view:

from django.shortcuts import reverse
from admin_auto_filters.filters import AutocompleteFilter


class ArtistFilter(AutocompleteFilter):
    title = 'Artist'
    field_name = 'artist'

    def get_autocomplete_url(self, request, model_admin):
        return reverse('admin:custom_search')

Shortcut for creating filters:

It's also possible to use the AutocompleteFilterFactory shortcut to create filters on the fly, as shown below. Nested relations are supported too, with no need to specify the model.

from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilterFactory


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True)
    ]

    def get_urls(self):
        """As above..."""

Customizing widget text

You can customize the text displayed in the filter widget, to use something other than str(obj). This needs to be configured for both the dropdown endpoint and the widget itself.

In your views.py, override display_text:

from admin_auto_filters.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):

    @staticmethod
    def display_text(obj):
        return obj.my_str_method()

    def get_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 your admin.py:

from django import forms
from django.contrib import admin
from django.shortcuts import reverse
from admin_auto_filters.filters import AutocompleteFilter


class FoodChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.my_str_method()


class ArtistFilter(AutocompleteFilter):
    title = 'Artist'
    field_name = 'artist'
    form_field = FoodChoiceField

    def get_autocomplete_url(self, request, model_admin):
        return reverse('admin:custom_search')


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]

    def get_urls(self):
        """As above..."""

Option two is to use an AutocompleteFilterFactory in your admin.py add a label_by argument:

from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilterFactory


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True, label_by='my_str_method')
    ]

    def get_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 branch pre_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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_autocomplete_filters-0.8.0rc2.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file django_admin_autocomplete_filters-0.8.0rc2.tar.gz.

File metadata

File hashes

Hashes for django_admin_autocomplete_filters-0.8.0rc2.tar.gz
Algorithm Hash digest
SHA256 56d80e396864c8aaeb7b2f49377f15bc46237d3ba9905015aa8e1e86d0aed726
MD5 e388b1882b79ee7c654d603b70fcebef
BLAKE2b-256 75b5bdeee28124434aecb93c18ab2cc6cc53f8b125322cc0e6eca1726c2750f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_autocomplete_filters-0.8.0rc2.tar.gz:

Publisher: publish.yml on Barsoomx/django-admin-autocomplete-filters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_admin_autocomplete_filters-0.8.0rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_autocomplete_filters-0.8.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 44a87141d01a2421ef9520226441c9669bd4ac6b9fc8aeab380aecb5602aacac
MD5 ea41a8f292a9322500e9877fd0246de5
BLAKE2b-256 7bf7fd381d88d0de92313998958bbd934e831c8ffa2330256e710b627fe9c23f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_autocomplete_filters-0.8.0rc2-py3-none-any.whl:

Publisher: publish.yml on Barsoomx/django-admin-autocomplete-filters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page