Ajax autocomplete list filter for Django admin
Project description
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!
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
Contributer(s)
- Uğur "vigo" Özyılmazel - Author, Maintainer
- Can Adıyaman - Author, Maintainer
- Erdi Mollahüseyinoğlu - Author, Maintainer
Contribute
All PR’s are welcome!
fork
(https://github.com/demiroren-teknoloji/django-admin-autocomplete-list-filter/fork)- Create your
branch
(git checkout -b my-features
) commit
yours (git commit -am 'added killer options'
)push
yourbranch
(git push origin my-features
)- Than create a new Pull Request!
TODO
- Add unit tests
- Improve JavaScript code :)
Change Log
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.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size django_admin_autocomplete_list_filter-0.1.3-py3-none-any.whl (7.8 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size django-admin-autocomplete-list-filter-0.1.3.tar.gz (6.8 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for django_admin_autocomplete_list_filter-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3f4ddbaa6b9e9862ae6c44029db3548a25d73f92bd619c5246e2c62ee6ac6c6 |
|
MD5 | e04207fcfbaecb7685fd86963d6ab33f |
|
BLAKE2-256 | ff2a83b08ef2b30c5031db78945de88366e3ec9bf06b03a6f71d098bd296da4a |
Hashes for django-admin-autocomplete-list-filter-0.1.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a02f297b13900ee5758ba79d861129b1f18d8bc5c768df9ef371eafdaf366e4a |
|
MD5 | afec39a3dcce780457daa94212d87471 |
|
BLAKE2-256 | bd3468ca547836bcb82ed744ceb0ddd8bb8072937c5a0bde680aa2d20fca2947 |