3-Level Filter-By Example

Example Overview

The “3-Level Filter-By” example demonstrates how to use django_tomselect to implement a hierarchical selection process, where options at one level depend on the user’s choice in the previous level. This is ideal for use cases like selecting regions, countries, and local markets, where each level dynamically filters based on the prior selection.

Objective:

  • To showcase a multi-tiered filtering process in forms using TomSelectModelChoiceField.

  • Demonstrates the power of django_tomselect plugins like PluginDropdownHeader for organizing data.

Use Case:

  • Content publishing platforms selecting publishing regions, countries, and local markets.

  • Applications that require dependent dropdowns like car make >> model >> trim selection.

Visual Examples

Screenshot: 3-Level Filter-By


Key Code Segments

Forms

The form leverages TomSelectModelChoiceField to define hierarchical filtering.


Templates

The form fields are rendered dynamically in the template.


Autocomplete Views

Dynamic filtering logic is handled by autocomplete endpoints in the backend.


Design and Implementation Notes

Key Features

  • Dynamic Filtering: Each dropdown is filtered based on the selected value of the parent dropdown. Trying to select a country without a region will show an empty list. Likewise, trying to select a local market without a country will also show an empty list.

  • Header Customization: PluginDropdownHeader adds informative headers with columns like “Potential Readers” and “Markets.”

Design Decisions

  • Using hierarchical filtering ensures scalability for large datasets without loading all options at once.

  • Plugin configurations like extra_columns enhance the dropdown’s usability by providing rich, tabular data.