SmartBase Admin supports Global Filters, which allow users to apply filtering across all views (e.g., dashboards, lists) in the admin UI. These filters appear in the sidebar and affect queryset evaluation automatically.
In the example below, a global domain selector is implemented. This allows switching between data scopes like www.domain1.com
, www.domain2.com
, or “All domains”.
The global filter form is defined using a standard Django form. Django SmartBase Admin automatically renders it in the sidebar and handles session-based storage for the selected values.
💡 Example: Domain Filter 1. Define the Global Filter Formsb_admin_configuration.py
from django import forms
from django_smartbase_admin.admin.widgets import SBAdminRadioWidget
from project.catalog.models import Domain
class GlobalFilterForm(forms.Form):
include_all_values_for_empty_fields = ["domain"]
domain = forms.ModelChoiceField(
queryset=Domain.objects.all(),
required=False,
blank=True,
widget=SBAdminRadioWidget(attrs={"id": "DOMAIN_FILTER"}),
empty_label="All domains",
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["domain"].label_from_instance = self.label
@staticmethod
def label(obj):
return obj.name
include_all_values_for_empty_fields
: Ensures that all values are included even if no selection is made.SBAdminRadioWidget
: Displays the filter as a radio selection in the sidebar.Selections are automatically stored in the session and persist across page changes.
2. Hook the Filter into ConfigurationAssign the global filter form to your base configuration:
sb_admin_configuration.py
class SBAdminConfiguration(SBAdminConfigurationBase):
global_filter_form = GlobalFilterForm
...
3. Apply Filter in Querysets
Override restrict_queryset()
to apply the global filter to domain-scoped models.
sb_admin_configuration.py
from django_smartbase_admin.engine.const import GLOBAL_FILTER_DATA_KEY
class SBAdminConfiguration(SBAdminConfigurationBase):
global_filter_form = GlobalFilterForm
def restrict_queryset(
self,
qs,
model,
request,
request_data,
global_filter=True,
global_filter_data_map=None,
):
qs = super().restrict_queryset(qs, model, request, request_data, global_filter, global_filter_data_map)
global_filter_map = request.session.get(GLOBAL_FILTER_DATA_KEY)
if global_filter_map:
domain = global_filter_map.get("domain")
if issubclass(model, BaseDomainModel) and domain:
qs = qs.filter(domain_id=domain)
return qs
This ensures that only the data belonging to the selected domain is displayed in dashboards and lists.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4