├── tests ├── __init__.py ├── app │ ├── __init__.py │ ├── urls.py │ ├── apps.py │ ├── admin.py │ ├── models.py │ └── settings.py ├── django_admin_multi_select_filter │ ├── __init__.py │ ├── conftest.py │ └── test_filters.py └── factories.py ├── src └── django_admin_multi_select_filter │ ├── __init__.py │ └── filters.py ├── .github └── workflows │ ├── python-publish.yml │ └── test.yml ├── README.md ├── LICENSE ├── tox.ini ├── pyproject.toml └── .gitignore /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/app/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/django_admin_multi_select_filter/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/django_admin_multi_select_filter/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/app/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path 3 | 4 | urlpatterns = [path("admin/", admin.site.urls)] 5 | -------------------------------------------------------------------------------- /tests/app/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class TestAppConfig(AppConfig): 5 | name = "tests.app" 6 | label = "testapp" 7 | -------------------------------------------------------------------------------- /tests/django_admin_multi_select_filter/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from tests.factories import ItemFactory, TagFactory 4 | 5 | 6 | @pytest.fixture 7 | def sample_data(): 8 | a = TagFactory(name="A") 9 | b = TagFactory(name="B") 10 | c = TagFactory(name="C") 11 | 12 | i1 = ItemFactory(name="i1", tags=[a, b]) 13 | i2 = ItemFactory(name="i2", tags=[a]) 14 | i3 = ItemFactory(name="i3", tags=[b, c]) 15 | i4 = ItemFactory(name="i4") 16 | 17 | return {"tags": (a, b, c), "items": (i1, i2, i3, i4)} 18 | -------------------------------------------------------------------------------- /tests/app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from django_admin_multi_select_filter.filters import MultiSelectFieldListFilter, MultiSelectRelatedFieldListFilter 4 | from tests.app.models import Item, Tag 5 | 6 | 7 | @admin.register(Item) 8 | class ItemAdmin(admin.ModelAdmin): 9 | list_display = ("name",) 10 | list_filter = ( 11 | ("tags", MultiSelectRelatedFieldListFilter), 12 | ("status", MultiSelectFieldListFilter), 13 | ) 14 | 15 | 16 | @admin.register(Tag) 17 | class TagAdmin(admin.ModelAdmin): 18 | list_display = ("name",) 19 | -------------------------------------------------------------------------------- /tests/app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Tag(models.Model): 5 | name = models.CharField(max_length=100) 6 | 7 | def __str__(self): 8 | return self.name 9 | 10 | 11 | class Item(models.Model): 12 | name = models.CharField(max_length=100) 13 | tags = models.ManyToManyField(Tag, blank=True) 14 | status = models.CharField( 15 | max_length=10, 16 | choices=[ 17 | ("new", "New"), 18 | ("old", "Old"), 19 | ("archived", "Archived"), 20 | ], 21 | blank=True, 22 | default="", 23 | ) 24 | 25 | def __str__(self): 26 | return self.name 27 | -------------------------------------------------------------------------------- /tests/factories.py: -------------------------------------------------------------------------------- 1 | import factory 2 | from factory.django import DjangoModelFactory 3 | 4 | from tests.app.models import Item, Tag 5 | 6 | 7 | class TagFactory(DjangoModelFactory): 8 | class Meta: 9 | model = Tag 10 | 11 | name = factory.Sequence(lambda n: f"tag-{n}") 12 | 13 | 14 | class ItemFactory(DjangoModelFactory): 15 | class Meta: 16 | model = Item 17 | 18 | name = factory.Sequence(lambda n: f"item-{n}") 19 | 20 | @factory.post_generation 21 | def tags(self, create, extracted, **kwargs): 22 | if not create: 23 | return 24 | if extracted: 25 | for t in extracted: 26 | self.tags.add(t) 27 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | name: Upload Python Package 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | permissions: 8 | contents: read 9 | 10 | jobs: 11 | deploy: 12 | 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Set up Python 18 | uses: actions/setup-python@v5 19 | with: 20 | python-version: '3.10' 21 | - name: Install dependencies 22 | run: | 23 | python -m pip install --upgrade pip 24 | pip install build 25 | - name: Build package 26 | run: python -m build 27 | - name: Publish package 28 | uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 29 | with: 30 | user: __token__ 31 | password: ${{ secrets.PYPI_API_TOKEN }} 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django admin multi-select filter 2 | 3 | Django admin multi-select filter is a Django app that allows you to add a multi-select filter to the Django admin. 4 | 5 | ## Installation 6 | 1. Install using pip: 7 | ```bash 8 | pip install django-admin-multi-select-filter 9 | ``` 10 | 2. Use the `MultiSelectFilter` (or `MultiSelectRelatedFieldListFilter` when using on related fields) in your admin classes (you do **not** need to add the app to `INSTALLED_APPS`): 11 | ```python 12 | from django.contrib import admin 13 | from django_admin_multi_select_filter.filters import MultiSelectFieldListFilter 14 | 15 | class MyModelAdmin(admin.ModelAdmin): 16 | list_filter = ( 17 | ... 18 | ('my_field', MultiSelectFieldListFilter), 19 | ... 20 | ) 21 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Job Doesburg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /tests/app/settings.py: -------------------------------------------------------------------------------- 1 | SECRET_KEY = "test" 2 | USE_TZ = True 3 | TIME_ZONE = "UTC" 4 | 5 | INSTALLED_APPS = [ 6 | "django.contrib.auth", 7 | "django.contrib.contenttypes", 8 | "django.contrib.sessions", 9 | "django.contrib.messages", 10 | "django.contrib.admin", 11 | "tests.app", 12 | ] 13 | 14 | MIDDLEWARE = [ 15 | "django.middleware.security.SecurityMiddleware", 16 | "django.contrib.sessions.middleware.SessionMiddleware", 17 | "django.middleware.common.CommonMiddleware", 18 | "django.middleware.csrf.CsrfViewMiddleware", 19 | "django.contrib.auth.middleware.AuthenticationMiddleware", 20 | "django.contrib.messages.middleware.MessageMiddleware", 21 | ] 22 | 23 | ROOT_URLCONF = "tests.app.urls" 24 | 25 | TEMPLATES = [ 26 | { 27 | "BACKEND": "django.template.backends.django.DjangoTemplates", 28 | "DIRS": [], 29 | "APP_DIRS": True, 30 | "OPTIONS": { 31 | "context_processors": [ 32 | "django.template.context_processors.request", 33 | "django.contrib.auth.context_processors.auth", 34 | "django.contrib.messages.context_processors.messages", 35 | ] 36 | }, 37 | } 38 | ] 39 | 40 | DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}} 41 | STATIC_URL = "/static/" 42 | 43 | MIGRATION_MODULES = {"tests.app": None} 44 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = 3 | py39-django42 4 | py310-django{42,52} 5 | py311-django{42,52} 6 | py312-django{42,52} 7 | py313-django52 8 | pyproject, 9 | lint 10 | format 11 | 12 | labels = 13 | pyproject = pyproject, 14 | lint = lint, 15 | format = format, 16 | py39 = py39-django42 17 | py310 = py310-django{42,52} 18 | py311 = py311-django{42,52} 19 | py312 = py312-django{42,52} 20 | py313 = py313-django52 21 | 22 | [testenv] 23 | setenv = 24 | PYTHONUTF8 = 1 25 | REPORT_NAME = {env:TOX_ENV:{envname}} 26 | 27 | [testenv:py3{9,10,11,12,13}-django{42,52}] 28 | extras = 29 | django42: django42 30 | django52: django52 31 | test 32 | allowlist_externals = 33 | coverage 34 | commands = 35 | pytest \ 36 | --cov django_admin_multi_select_filter \ 37 | --cov-report term-missing \ 38 | --cov-append \ 39 | --junitxml=.reports/{env:REPORT_NAME}_junit.xml \ 40 | {posargs} 41 | commands_post = 42 | coverage xml -o .reports/{env:REPORT_NAME}_coverage.xml 43 | 44 | [testenv:pyproject] 45 | skip_install = true 46 | deps = 47 | validate-pyproject[all]~=0.12.1 48 | commands = 49 | validate-pyproject pyproject.toml 50 | 51 | [testenv:lint] 52 | skip_install = true 53 | deps = 54 | ruff~=0.12.0 55 | commands = 56 | ruff check . --output-format=full 57 | 58 | [testenv:format] 59 | skip_install = true 60 | deps = 61 | ruff~=0.9.0 62 | commands = 63 | ruff format --diff . 64 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: django-admin-multi-select-filter-test 2 | on: [push] 3 | env: 4 | TEST_DEPENDENCIES: "tox~=4.15.1" 5 | jobs: 6 | check-project-files: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | - name: variables 11 | run: | 12 | PROJECT_FILES=(README.md pyproject.toml tox.ini) 13 | ERROR=0 14 | - name: test 15 | run: | 16 | for file in ${PROJECT_FILES[*]}; do 17 | if ! [ -f $file ]; then 18 | echo "$file not found in project" 19 | ERROR=1 20 | fi 21 | done 22 | exit $ERROR 23 | lint: 24 | runs-on: ubuntu-latest 25 | strategy: 26 | matrix: 27 | linters: [pyproject, lint, format] 28 | steps: 29 | - uses: actions/checkout@v4 30 | - name: Set up Python 31 | uses: actions/setup-python@v5 32 | with: 33 | python-version: '3.13' 34 | cache: 'pip' 35 | - name: install-dependencies 36 | run: | 37 | python3 -m pip install --upgrade pip 38 | pip install $TEST_DEPENDENCIES 39 | - name: test 40 | run: tox -e "${{ matrix.linters }}" 41 | test: 42 | runs-on: ubuntu-latest 43 | strategy: 44 | matrix: 45 | minor_versions: [9, 10, 11, 12, 13] 46 | steps: 47 | - uses: actions/checkout@v4 48 | - name: Set up Python 49 | uses: actions/setup-python@v5 50 | with: 51 | python-version: '3.${{ matrix.minor_versions }}' 52 | - name: install-dependencies 53 | run: | 54 | python3 -m pip install --upgrade pip 55 | pip install $TEST_DEPENDENCIES 56 | - name: test 57 | run: tox -m "py3${{ matrix.minor_versions }}" 58 | - name: Upload coverage XML 59 | if: always() 60 | uses: actions/upload-artifact@v4 61 | with: 62 | name: coverage-${{ matrix.minor_versions }} 63 | include-hidden-files: true 64 | path: | 65 | ./.reports/*_coverage.xml -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "django-admin-multi-select-filter" 3 | version = "1.5.0" 4 | description = "Django admin filter for multiple select" 5 | readme = "README.md" 6 | authors = [{ name = "Job Doesburg", email = "job.doesburg@gmail.com" }] 7 | license = { file = "LICENSE" } 8 | requires-python = ">=3" 9 | classifiers = [ 10 | "License :: OSI Approved :: MIT License", 11 | "Programming Language :: Python", 12 | "Programming Language :: Python :: 3", 13 | "Framework :: Django", 14 | "Framework :: Django :: 3.0", 15 | "Framework :: Django :: 3.1", 16 | "Framework :: Django :: 3.2", 17 | "Framework :: Django :: 4.0", 18 | "Framework :: Django :: 4.1", 19 | "Framework :: Django :: 4.2", 20 | "Framework :: Django :: 5.0", 21 | "Intended Audience :: Developers", 22 | "Operating System :: OS Independent", 23 | ] 24 | 25 | dependencies = [ 26 | "django>=3", 27 | ] 28 | 29 | [project.optional-dependencies] 30 | django42 = ["django~=4.2.0"] 31 | django52 = ["django~=5.2.0"] 32 | test = ["pytest", "pytest-django", "factory_boy", "pytest-cov"] 33 | 34 | [project.urls] 35 | homepage = "https://github.com/JobDoesburg/django-admin-multi-select-filter" 36 | repository = "https://github.com/JobDoesburg/django-admin-multi-select-filter" 37 | documentation = "https://github.com/JobDoesburg/django-admin-multi-select-filter" 38 | 39 | [build-system] 40 | requires = ["setuptools>=61.0.0", "wheel"] 41 | build-backend = "setuptools.build_meta" 42 | 43 | [tool.setuptools.packages.find] 44 | where = ["src"] 45 | 46 | [tool.setuptools.package-data] 47 | "*" = ["*.html"] 48 | 49 | [tool.ruff] 50 | line-length = 119 51 | output-format = "grouped" 52 | show-fixes = true 53 | target-version = "py313" 54 | exclude = [".svn", "CVS", ".bzr", ".hg",".git", "__pycache__", ".tox", ".eggs", "*.egg", ".venv", "env", "venv", "build"] 55 | 56 | [tool.ruff.lint] 57 | select = ["W", "E", "F", "I", "N", "DJ", "T20", "Q"] 58 | 59 | [tool.ruff.lint.isort] 60 | combine-as-imports = true 61 | 62 | [tool.ruff.lint.mccabe] 63 | max-complexity = 6 64 | 65 | [tool.pytest.ini_options] 66 | DJANGO_SETTINGS_MODULE = "tests.app.settings" 67 | addopts = "-ra -q" 68 | pythonpath =". src" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /tests/django_admin_multi_select_filter/test_filters.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from django.urls import reverse 3 | 4 | from tests.app.models import Item 5 | from tests.factories import ItemFactory 6 | 7 | pytestmark = pytest.mark.django_db 8 | 9 | 10 | class TestMultiSelectRelatedFieldListFilter: 11 | def test_in_param_comma_separated_parses_lookup_val(self, admin_client, sample_data): 12 | url = reverse("admin:testapp_item_changelist") 13 | a, b, _ = sample_data["tags"] 14 | resp = admin_client.get(url, {"tags__id__in": f"{a.pk},{b.pk}"}) 15 | assert resp.status_code == 200 16 | 17 | spec = self._get_filter_spec(resp) 18 | 19 | assert spec.lookup_kwarg == "tags__id__in" 20 | assert spec.lookup_kwarg_isnull == "tags__isnull" 21 | assert spec.lookup_val == [str(a.pk), str(b.pk)] 22 | 23 | qs = list(resp.context["cl"].queryset) 24 | i1, i2, i3, i4 = sample_data["items"] 25 | assert set(qs) == {i1, i2, i3} 26 | 27 | def test_isnull_true_filters_items_without_relations(self, admin_client, sample_data): 28 | url = reverse("admin:testapp_item_changelist") 29 | resp = admin_client.get(url, {"tags__isnull": "1"}) 30 | assert resp.status_code == 200 31 | 32 | spec = self._get_filter_spec(resp) 33 | assert spec.lookup_kwarg_isnull == "tags__isnull" 34 | 35 | qs = list(resp.context["cl"].queryset) 36 | i1, i2, i3, i4 = sample_data["items"] 37 | assert qs == [i4] 38 | 39 | def test_empty_value_leaves_queryset_unfiltered(self, admin_client, sample_data): 40 | url = reverse("admin:testapp_item_changelist") 41 | resp = admin_client.get(url) 42 | assert resp.status_code == 200 43 | 44 | spec = self._get_filter_spec(resp) 45 | assert spec.lookup_val == [] 46 | 47 | qs = list(resp.context["cl"].queryset) 48 | assert set(qs) == set(Item.objects.all()) 49 | 50 | def _get_filter_spec(self, response, filter_cls_name="MultiSelectRelatedFieldListFilter"): 51 | cl = response.context["cl"] 52 | for spec in cl.filter_specs: 53 | if spec.__class__.__name__ == filter_cls_name: 54 | return spec 55 | 56 | 57 | class TestMultiSelectFieldListFilter: 58 | def test_in_param_comma_separated_parses_lookup_val(self, admin_client): 59 | item_new = ItemFactory(name="A", status="new") 60 | item_old = ItemFactory(name="B", status="old") 61 | ItemFactory(name="C", status="archived") 62 | ItemFactory(name="D", status="") 63 | 64 | url = reverse("admin:testapp_item_changelist") 65 | 66 | resp = admin_client.get(url, {"status__in": "new,old"}) 67 | assert resp.status_code == 200 68 | 69 | spec = self._get_filter_spec(resp) 70 | assert spec.lookup_kwarg == "status__in" 71 | assert spec.lookup_kwarg_isnull == "status__isnull" 72 | assert spec.lookup_val == ["new", "old"] 73 | 74 | qs = resp.context["cl"].queryset 75 | assert not qs.exclude(status__in=["new", "old"]).exists() 76 | assert {item_new, item_old}.issubset(set(qs)) 77 | 78 | def test_isnull_true_filters_items_with_nulls(self, admin_client): 79 | ItemFactory(name="E", status="") 80 | ItemFactory(name="F", status="new") 81 | url = reverse("admin:testapp_item_changelist") 82 | resp = admin_client.get(url, {"status__isnull": "1"}) 83 | assert resp.status_code == 200 84 | 85 | spec = self._get_filter_spec(resp) 86 | assert spec.lookup_kwarg_isnull == "status__isnull" 87 | 88 | qs = resp.context["cl"].queryset 89 | 90 | assert not qs.filter(status__isnull=False).exists() 91 | 92 | def test_empty_value_leaves_queryset_unfiltered(self, admin_client): 93 | ItemFactory(name="G", status="new") 94 | ItemFactory(name="H", status="") 95 | url = reverse("admin:testapp_item_changelist") 96 | 97 | resp = admin_client.get(url) 98 | assert resp.status_code == 200 99 | 100 | spec = self._get_filter_spec(resp) 101 | assert spec.lookup_val == [] 102 | 103 | qs = list(resp.context["cl"].queryset) 104 | assert set(qs) == set(Item.objects.all()) 105 | 106 | def test_empty_string_param_turns_into_empty_selection(self, admin_client): 107 | url = reverse("admin:testapp_item_changelist") 108 | 109 | resp = admin_client.get(url, {"status__in": ""}) 110 | assert resp.status_code == 200 111 | 112 | spec = self._get_filter_spec(resp) 113 | assert spec.lookup_val == [] 114 | 115 | def _get_filter_spec(self, response): 116 | cl = response.context["cl"] 117 | for spec in cl.filter_specs: 118 | if spec.__class__.__name__ == "MultiSelectFieldListFilter": 119 | return spec 120 | -------------------------------------------------------------------------------- /src/django_admin_multi_select_filter/filters.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.contrib.admin.options import IncorrectLookupParameters 3 | from django.contrib.admin.utils import reverse_field_path 4 | from django.core.exceptions import ValidationError 5 | from django.db.models import Count, Q 6 | from django.utils.translation import gettext_lazy as _ 7 | 8 | 9 | class MultiSelectFieldListFilter(admin.FieldListFilter): 10 | def __init__(self, field, request, params, model, model_admin, field_path): 11 | self.lookup_kwarg = field_path + "__in" 12 | self.lookup_kwarg_isnull = field_path + "__isnull" 13 | 14 | super().__init__(field, request, params, model, model_admin, field_path) 15 | 16 | self.lookup_val = self.used_parameters.get(self.lookup_kwarg, []) 17 | if len(self.lookup_val) == 1 and (self.lookup_val[0] == [""] or self.lookup_val[0] == ""): 18 | self.lookup_val = [] 19 | elif len(self.lookup_val) == 1 and not isinstance(self.lookup_val[0], str): 20 | self.lookup_val = self.lookup_val[0] 21 | self.lookup_val_isnull = self.used_parameters.get(self.lookup_kwarg_isnull) 22 | 23 | self.empty_value_display = model_admin.get_empty_value_display() 24 | parent_model, reverse_path = reverse_field_path(model, field_path) 25 | 26 | if model == parent_model: 27 | queryset = model_admin.get_queryset(request) 28 | else: 29 | queryset = parent_model._default_manager.all() 30 | self.lookup_choices = queryset.distinct().order_by(field.name).values_list(field.name, flat=True) 31 | self.field_verboses = {} 32 | if self.field.choices: 33 | self.field_verboses = {field_value: field_verbose for field_value, field_verbose in self.field.choices} 34 | 35 | def expected_parameters(self): 36 | return [self.lookup_kwarg, self.lookup_kwarg_isnull] 37 | 38 | def choices(self, changelist): 39 | yield { 40 | "selected": not self.lookup_val and self.lookup_val_isnull is None, 41 | "query_string": changelist.get_query_string(remove=[self.lookup_kwarg, self.lookup_kwarg_isnull]), 42 | "display": _("All"), 43 | } 44 | include_none = False 45 | for val in self.lookup_choices: 46 | if val is None: 47 | include_none = True 48 | continue 49 | val = str(val) 50 | 51 | if val in self.lookup_val: 52 | values = [v for v in self.lookup_val if v != val] 53 | else: 54 | values = self.lookup_val + [val] 55 | 56 | if values: 57 | yield { 58 | "selected": val in self.lookup_val, 59 | "query_string": changelist.get_query_string( 60 | {self.lookup_kwarg: ",".join(values)}, 61 | [self.lookup_kwarg_isnull], 62 | ), 63 | "display": self.field_verboses.get(val, val), 64 | } 65 | else: 66 | yield { 67 | "selected": val in self.lookup_val, 68 | "query_string": changelist.get_query_string(remove=[self.lookup_kwarg]), 69 | "display": self.field_verboses.get(val, val), 70 | } 71 | 72 | if include_none: 73 | yield { 74 | "selected": bool(self.lookup_val_isnull), 75 | "query_string": changelist.get_query_string({self.lookup_kwarg_isnull: "True"}, [self.lookup_kwarg]), 76 | "display": self.empty_value_display, 77 | } 78 | 79 | 80 | class MultiSelectRelatedFieldListFilter(admin.RelatedFieldListFilter): 81 | def __init__(self, field, request, params, model, model_admin, field_path): 82 | super().__init__(field, request, params, model, model_admin, field_path) 83 | self.lookup_kwarg = "%s__%s__in" % (field_path, field.target_field.name) 84 | self.lookup_kwarg_isnull = "%s__isnull" % field_path 85 | 86 | values = request.GET.getlist(self.lookup_kwarg) 87 | if len(values) == 1 and "," in values[0]: 88 | values = values[0].split(",") 89 | self.lookup_val = [str(value) for value in values if value] 90 | 91 | self.lookup_choices = self.field_choices(field, request, model_admin) 92 | 93 | def choices(self, changelist): 94 | yield { 95 | "selected": (self.lookup_val is None or self.lookup_val == []) and not self.lookup_val_isnull, 96 | "query_string": changelist.get_query_string(remove=[self.lookup_kwarg, self.lookup_kwarg_isnull]), 97 | "display": _("All"), 98 | } 99 | 100 | for pk_val, val in self.lookup_choices: 101 | if val is None: 102 | self.include_empty_choice = True 103 | continue 104 | val = str(val) 105 | 106 | if str(pk_val) in self.lookup_val: 107 | values = [str(v) for v in self.lookup_val if str(v) != str(pk_val)] 108 | else: 109 | values = self.lookup_val + [str(pk_val)] 110 | 111 | yield { 112 | "selected": self.lookup_val is not None and str(pk_val) in self.lookup_val, 113 | "query_string": changelist.get_query_string( 114 | {self.lookup_kwarg: ",".join(values)}, [self.lookup_kwarg_isnull] 115 | ), 116 | "display": val, 117 | } 118 | empty_title = self.empty_value_display 119 | if self.include_empty_choice: 120 | yield { 121 | "selected": bool(self.lookup_val_isnull), 122 | "query_string": changelist.get_query_string({self.lookup_kwarg_isnull: "True"}, [self.lookup_kwarg]), 123 | "display": empty_title, 124 | } 125 | 126 | 127 | class ExclusiveMultiSelectRelatedFieldListFilter(MultiSelectRelatedFieldListFilter): 128 | def queryset(self, request, queryset): 129 | try: 130 | if self.lookup_val_isnull: 131 | return queryset.filter(**{self.lookup_kwarg_isnull: True}) 132 | 133 | choices = self.lookup_val 134 | choice_len = len(choices) 135 | if choice_len == 0: 136 | return queryset 137 | 138 | queryset = queryset.alias( 139 | nmatch=Count(self.field_path, filter=Q(**{f"{self.lookup_kwarg}": choices}), distinct=True) 140 | ).filter(nmatch=choice_len) 141 | return queryset 142 | 143 | except (ValueError, ValidationError) as e: 144 | raise IncorrectLookupParameters(e) 145 | --------------------------------------------------------------------------------