Skip to main content

django_select2_admin_filters

This extension is based on django-select2 and works with or without Grappelli.

Installation

  • Install using pip

    pip install django-select2-admin-filters
    
  • Update INSTALLED_APPS, you need too put django_select2_admin_filters after admin and django_select2

    INSTALLED_APPS = [
        'django.contrib.admin',
        #
        'django_select2',
        'django_select2_admin_filters',
    ]
    
  • Update urls.py to use model filters (refer to django-select2 documentation)

    path('select2/', include('django_select2.urls')),
    

Filters

Filters are generally of two types, but each of them can be single or multiple selectable:

  • ChoiceFilter
    • ChoiceSelect2Filter
    • MultipleChoiceSelect2Filter
  • ModelFilter
    • ModelSelect2Filter
    • MultipleModelSelect2Filter

Usage

  • Use filter in your admin.py
      from django.contrib import admin
      from django_select2_admin_filters.admin import (
          Select2AdminFilterMixin)
      from django_select2_admin_filters.filters import (
          ChoiceSelect2Filter, MultipleChoiceSelect2Filter,
          ModelSelect2Filter, MultipleModelSelect2Filter)
      from your_app.models import Country, Person, Profession
    
    
      class CountryFilter(ModelSelect2Filter):
          title = 'Country of residence'                 # filter's title
          parameter_name = 'country'                     # parameter used in url and by default field name of Foreign Key used to filter results
          autocomplete_queryset = Country.objects.all()  # queryset to autocomplete
          search_fields = ['name__icontains']            # fields of Country model used to filtering
    
          # optionally you can override queryset method
          def queryset(self, request, queryset):
              val = self.value()
              if val:
                  return queryset.filter(country_of_residence=val)
              return queryset
    
    
      class ProfessionFilter(MultipleModelSelect2Filter):
          title = 'Profession'
          parameter_name = 'profession'
          autocomplete_queryset = Profession.objects.all()
          search_fields = ['name__icontains']
    
          def queryset(self, request, queryset):
              val = self.value_as_list()
              if len(val) > 0:
                  return queryset.filter(professions__profession_id__in=val)
              return queryset
    
    
      class StatusFilter(ChoiceSelect2Filter):
          title = 'Status'
          parameter_name = 'status'
          autocomplete_choice_list = [    # list of choices
              (1, 'Active',),
              (2, 'Suspended',),
              (3, 'Deleted',),
          ]
    
    
      @admin.register(Person)
      class PersonAdmin(Select2AdminFilterMixin, admin.ModelAdmin):
    
          # change_list_template = 'admin/change_list_filter_sidebar.html' <- DON'T override change_list_template
          list_filter = (CountryFilter, ProfessionFilter, StatusFilter,) # actually you cannot mix filters with traditional filters
    

TODO

  • add tests
  • add handling dependent_fields

Author

Metadata

Release files for django-select2-admin-filters 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-select2-admin-filters 0.1.4
File Size Uploaded Provenance Actions
django-select2-admin-filters-0.1.4.tar.gz 7.1 kB Without provenance Details for django-select2-admin-filters-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-select2-admin-filters 0.1.4
File Interpreter ABI Platform Actions
django_select2_admin_filters-0.1.4-py2-none-any.whl Python 2 none any Details for django_select2_admin_filters-0.1.4-py2-none-any.whl

Total release size: 17.2 kB

Release files / django-select2-admin-filters-0.1.4.tar.gz

Download URL django-select2-admin-filters-0.1.4.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
98670c784f7ad750c37828f9401c541386d8188da32c0a1cc369c8cdd0c58444
BLAKE2b-256 checksum
How to use checksums
ce76aaec26f3d1144b7a0ea46fd9d089da5418378f21b513f51cea726edf29fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Release files / django_select2_admin_filters-0.1.4-py2-none-any.whl

Download URL django_select2_admin_filters-0.1.4-py2-none-any.whl
Size 10.1 kB
Tags Python 2
SHA-256 checksum
How to use checksums
4bda23817854d78554deb1f473ebf143247b98cd27ebf4623e24a1cea8c8c6ef
BLAKE2b-256 checksum
How to use checksums
a78b441b4f61530dc56321ad0eb04971d06c47fbeccf164bb1765036059d6173
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Security for django-select2-admin-filters 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page