Constant Filter-By

Example Overview

This example filters options by constant values baked into the widget configuration rather than read from form fields, using the Const helper to always restrict articles to published ones while still allowing an optional magazine filter. Mixing constant and field-based filters lets you enforce business rules in the UI, such as only showing active items, in-stock products, or records for the current organization.

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

This template snippet is abridged. The live template also defines a {% block extra_header %} containing {{ form.media }} and a <style> block; without {{ form.media }} the widgets render unstyled.

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.