Constant Filter-By

Example Overview

  • Objective: This example demonstrates how to filter options using constant values that don’t come from form fields. The goal is to always filter articles to only show published ones, while optionally allowing additional filtering by magazine.

    • Problem Solved: When you need to enforce business rules in the UI (e.g., only show active items, only show published content), constant filters allow you to bake these rules into the widget configuration.

    • Features Highlighted:

      • The Const helper function for creating constant filter values.

      • Mixing constant and field-based filters in a single configuration.

  • Use Case:

    • Content management: Only show published articles to editors.

    • E-commerce: Only show in-stock products.

    • Multi-tenant applications: Always filter by current organization.

    • User management: Only show active users.

Key Code Segments

Forms

This example uses the Const helper to create a filter that always applies:

Explanation:

  • Const("published", "status") creates a filter that always applies status=published

  • The constant value is sent to the server regardless of any form field

  • This can be combined with regular field-based filters

  • When the magazine is selected, articles are filtered by BOTH the magazine AND the constant status

The Const Helper

The Const function is a convenience helper for creating constant filter specifications:

from django_tomselect.app_settings import Const, FilterSpec

# These are equivalent:
Const("published", "status")
FilterSpec(source="published", lookup="status", source_type="const")

Parameters:

  • value: The constant value to filter by (will be converted to string)

  • lookup: The Django ORM lookup field (e.g., “status”, “is_active”, “category_id”)

How It Works

When the form loads and the user searches for articles:

  1. The JavaScript always includes the constant filter in the URL:

    ?q=search&f='__const__status=published'
    
  2. If a magazine is also selected, both filters are included:

    ?q=search&f='magazine__magazine_id=5'&f='__const__status=published'
    
  3. The server recognizes the __const__ prefix and applies the filter directly

  4. Only published articles (optionally filtered by magazine) are returned

Templates

Design and Implementation Notes

  • Key Features:

    • Constant filters are always applied, regardless of form state

    • Can be mixed with field-based filters

    • Useful for enforcing business rules in the UI

  • Common Use Cases:

    Use Case

    Const Example

    Only published content

    Const("published", "status")

    Only active items

    Const("true", "is_active")

    Specific category

    Const("5", "category_id")

    Current year

    Const("2024", "year")

    Specific set of IDs (__in)

    Const([11, 13], "id__in")

    Numeric range (__range)

    Const([2020, 2024], "year__range")

    For list-valued lookups (__in, __range), pass a list or tuple as the value. Items are comma-joined into the URL parameter and split back into a list server-side before the queryset filter is applied. Items must not themselves contain commas; an empty list is rejected.

  • Security Note: While constant filters help enforce UI-level rules, always validate on the server side as well. Users could potentially modify the URL parameters.

See the Multiple Filter-By example for filtering by multiple form fields.