Skip to main content

Ajax autocomplete list filter for Django admin

Project description

Python Python Python Python Django Code style: black PyPI version

django-admin-autocomplete-list-filter

Ajax autocomplete list filter helper for Django admin. Uses Django’s built-in autocomplete widget! No extra package or install required!

After

Update

Dropped support for Django 2 family. Works with Django 3 or higher!. master branch is renamed to main... You can fix your existing clones via;

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

Installation and Usage

$ pip install django-admin-autocomplete-list-filter

Add djaa_list_filter to INSTALLED_APPS in your settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'djaa_list_filter',           
]

Now, let’s look at this example model:

# models.py

from django.conf import settings
from django.db import models


class Post(models.Model):
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE, related_name='posts')
    author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='posts')
    title = models.CharField(max_length=255)
    body = models.TextField()
    tags = models.ManyToManyField(to='Tag', blank=True)

    def __str__(self):
        return self.title


class Category(models.Model):
    title = models.CharField(max_length=255)

    def __str__(self):
        return self.title


class Tag(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name

We have 2 ForeignKey fields and one ManyToManyField to enable autocomplete list filter feature on admin. All you need is to inherit from AjaxAutocompleteListFilterModelAdmin which inherits from Django’s admin.ModelAdmin.

Now we have an extra ModelAdmin method: autocomplete_list_filter. Uses Django Admin’s search_fields logic. You need to enable search_fields in the related ModelAdmin. To enable completion on Category relation, CategoryAdmin should have search_fields that’s it!

from django.contrib import admin

from djaa_list_filter.admin import (
    AjaxAutocompleteListFilterModelAdmin,
)

from .models import Category, Post, Tag


@admin.register(Post)
class PostAdmin(AjaxAutocompleteListFilterModelAdmin):
    list_display = ('__str__', 'author', 'show_tags')
    autocomplete_list_filter = ('category', 'author', 'tags')

    def show_tags(self, obj):
        return ' , '.join(obj.tags.values_list('name', flat=True))


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    search_fields = ['title']
    ordering = ['title']


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    search_fields = ['name']
    ordering = ['name']

Development

You are very welcome to contribute, fix bugs or improve this project. We hope to help people who needs this feature. We made this package for our company project. Good appetite for all the Django developers out there!

License

This project is licensed under MIT


Contributor(s)


Contribute

All PR’s are welcome!

  1. fork (https://github.com/demiroren-teknoloji/django-admin-autocomplete-list-filter/fork)
  2. Create your branch (git checkout -b my-features)
  3. commit yours (git commit -am 'added killer options')
  4. push your branch (git push origin my-features)
  5. Than create a new Pull Request!

TODO

  • Add unit tests
  • Improve JavaScript code :)

Change Log

2021-08-17

2019-10-25

  • Remove f-string for older Python versions, will change this on 1.0.0 version

2019-10-19

  • Bump version: 0.1.2
  • Add Python 3.5 supports, thanks to Peter Farrel
  • Add animated gif :)
  • Add future warning for f-strings

2019-10-11

  • Add ManyToManyField support
  • Initial release

2019-10-07

  • Init repo...

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

Built Distribution

File details

Details for the file django-admin-autocomplete-list-filter-1.0.1.tar.gz.

File metadata

  • Download URL: django-admin-autocomplete-list-filter-1.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for django-admin-autocomplete-list-filter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 135d840cf337ad0f8823fbdb128cefaba65ae0867a87422b9f495f3fa50b204c
MD5 c22c4e8d7addf1dabd9373147c6234f8
BLAKE2b-256 6d355792ecd2d9226fd286a567de3f1696aad1c9956ac21a87e696c4ce20936c

See more details on using hashes here.

File details

Details for the file django_admin_autocomplete_list_filter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_autocomplete_list_filter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2252f75eb6c72f2bc2c98da84986584d8a202b8734d1e53c59cb5b36f52cb0f5
MD5 9e98af7aaee01641ab222e6d089b3e7a
BLAKE2b-256 0a6f8411ebc7c8452d12d23fa41194ab5f5326ec31221797e7679dee42a8831c

See more details on using hashes here.

Supported by

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