Go to file
Uğur Özyılmazel 873551e3d8
Fix image urls
2019-10-11 16:28:49 +03:00
djaa_list_filter Add ManyToMany and ForeignKey support 2019-10-11 10:33:41 +03:00
screenshots Add screenshots 2019-10-11 10:33:03 +03:00
.bumpversion.cfg Add python package scaffold 2019-10-07 14:54:39 +03:00
.flake8 Add linter, checker configurations 2019-10-11 10:34:01 +03:00
.gitignore POC works 2019-10-10 17:16:21 +03:00
.isort.cfg Add linter, checker configurations 2019-10-11 10:34:01 +03:00
LICENCE Add python package scaffold 2019-10-07 14:54:39 +03:00
MANIFEST.in Add MANIFEST file and update README file 2019-10-11 13:37:19 +03:00
README.md Fix image urls 2019-10-11 16:28:49 +03:00
Rakefile Add python package scaffold 2019-10-07 14:54:39 +03:00
pyproject.toml Add linter, checker configurations 2019-10-11 10:34:01 +03:00
setup.py Add MANIFEST file and update README file 2019-10-11 13:37:19 +03:00

README.md

Python Django Code style: black PyPI version

django-admin-autocomplete-list-filter

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

Before After
Before django-admin-autocomplete-list-filter After django-admin-autocomplete-list-filter

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, lets 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 Djangos admin.ModelAdmin.

Now we have an extra ModelAdmin method: autocomplete_list_filter. Uses Django Admins search_fields logic. You need to enable search_fields in the related ModelAdmin. To enable completion on Category relation, CategoryAdmin should have search_fields thats 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


Contributer(s)


Contribute

All PRs 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

2019-10-11

  • Add ManyToManyField support
  • Initial release

2019-10-07

  • Init repo...