layers Introducing Unfold components - Pre-made HTML blocks in Django admin for custom dashboards arrow_forward

Text filters

Text input field which allows filtering by the free string submitted by the user. There are two different variants of this filter: FieldTextFilter and TextFilter.

FieldTextFilter requires just a model field name and the filter will make __icontains search on this field. There are no other things to configure so the integration in list_filter will be just one new row looking like ("model_field_name", FieldTextFilter).

In the case of the TextFilter, it is needed to write a whole new class inheriting from TextFilter with a custom implementation of the queryset method and the parameter_name attribute. This attribute will be a representation of the search query parameter name in URI. The benefit of the TextFilter is the possibility of writing complex queries.

from django.contrib import admin
from django.contrib.auth.models import User
from django.core.validators import EMPTY_VALUES
from django.utils.translation import gettext_lazy as _
from unfold.admin import ModelAdmin
from unfold.contrib.filters.admin import TextFilter, FieldTextFilter

class CustomTextFilter(TextFilter):
    title = _("Custom filter")
    parameter_name = "query_param_in_uri"

    def queryset(self, request, queryset):
        if self.value() not in EMPTY_VALUES:
            # Here write custom query
            return queryset.filter(your_field=self.value())

        return queryset


@admin.register(User)
class MyAdmin(ModelAdmin):
    list_filter_submit = True  # Submit button at the bottom of the filter
    list_filter = [
        ("model_charfield", FieldTextFilter),
        CustomTextFilter
    ]

Be first to know about new features and updates

Each time something new happens in Unfold, like new major features or new design components, we'll send you a newsletter. We promise not to spam you.

Django admin theme built with Tailwind CSS to bring modern look and feel to your admin interface. Already contains several built-in features for smooth developer experience.

© 2023 - 2024 Created by unfoldadmin.com. All rights reserved.