Skip to main content

No project description provided

Project description

django filter groups

This package groups filters generated by django-filter.

The main reason to use it - show only selected filters.

It groups filters by their field_name. The filter lookup will be subtracted from filter_name. That's why int_field and int_field__isnull in example above, will be placed in ine group

class MyFilterSet(django_filters.FilterSet):
    int_field = django_filters.NumberFilter()
    int_field__isnull = django_filters.NumberFilter(lookup_expr="isnull")

A group name resolution goes the next way:

  • finding filter_group_label in any filter in current group
  • if multiple were found, the first would be selected
    • it's not good idea to add multiple filter_group_label's
    • the ways to add filter_group_label you can find here
  • if filter_group_label wasn't added, it's finding label declared in a filter with exact lookup expression
  • if it isn't presented, it'll get the first declared filter for this group and take its label
  • if no label is found, it'll get the field verbose_name from model
  • otherwise, it'll set "[invalid name]" group name, and it won't be able to choose such group

how to use

pip install django-filter-groups

add to your INSTALLED_APPS after django-filter

INSTALLED_APPS = [
  ...
  "django_filters",
  "django_filters_groups",
  ...
]

use it in your template:

  • add {% add_select_filter_form_to_context %} to the top of your template. It allows you to place {{ select_filter_form.media }} anywhere you want
  • add {% filters_by_groups %}
  • add {{ select_filter_form.media }}

If FilterSet name is not 'filter' -> add filterset to both tags
{% add_select_filter_form_to_context my_custom_filterset %}
{% filters_by_groups my_custom_filterset %} or filterset name {% filters_by_groups "my_custom_filterset" %} {% add_select_filter_form_to_context "my_custom_filterset" %}

warning

  • It's necessary to add values to exact and iexact VERBOSE_LOOKUPS.
    Filter choice will be empty if the lookups isn't presented
  • It's important to add filter verbose lookup in the end of declared filter label if filter_group_label wasn't added in any filter, and it's a filter with exact lookup or the first in a group
    Default groups of model fields declared in FilterSet._meta.fields have normal labels
# settings.py
# FILTERS_VERBOSE_LOOKUPS = {"exact": "match"}

class F(django_filters.FilterSet):
    my_number_filter = django_filters.NumberFilter(label="number match")  # good
    my_number_filter = django_filters.NumberFilter(label="number")  # bad

default settings

# django settings
FILTERS_GROUPS_SELECT_FILTER_FORM_LABEL = "Select a label"
// static/django_filters_groups/filter-defaults.js
let filterDefaults = {
  filterWrapperSelector: "p", // p is necessary when you use {{ form.as_p }}
  submitOnFilterDelete: false,
};

change group name

You can set group name directly in 2 ways:

  • add filter_group_label to your filter
class FFieldCountFilter(django_filters.NumberFilter):
    filter_group_label = "custom_group_label"
  • use get_filter_class_with_custom_label
from django_filters_groups.utils import get_filter_class_with_group_label

class MyFilterSet(django_filters.FilterSet):
  custom_filter = get_filter_class_with_group_label(django_filters.NumberFilter, "custom_group_label")(method="custom_filter_method")

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

django-filter-groups-0.4.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_filter_groups-0.4.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file django-filter-groups-0.4.3.tar.gz.

File metadata

  • Download URL: django-filter-groups-0.4.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.7 Windows/10

File hashes

Hashes for django-filter-groups-0.4.3.tar.gz
Algorithm Hash digest
SHA256 a9b4340e1f6f3096ede2a1440368ebfe51e0feec1a0908e3ad18f6e9e7f098f9
MD5 245fda6ad96c8822e4c61393c24482e1
BLAKE2b-256 68e17680d69bea75b8950656e391511f43ebb7d8427e299b791d859dbf7a3f8f

See more details on using hashes here.

File details

Details for the file django_filter_groups-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_filter_groups-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1b16752712eaa6681a50204323a73b24a3d3f62018c8c6d477cc8b9ac408aa95
MD5 67129f922c9b61094a7bd96999d29b25
BLAKE2b-256 e260f375fc96ad7fcf746147edeb998562d11e35994dfc01269cfefd67c45358

See more details on using hashes here.

Supported by

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