# Exclude-By Primary Author ## Example Overview - **Objective**: This example showcases how to dynamically exclude certain options in one field based on the selection in another field using `django_tomselect`. Specifically, the "Contributing Authors" field excludes the "Primary Author" selection to prevent redundant choices. Also, if no Primary Author is selected, the Contributing Authors field will remain empty. - **Problem Solved**: Maintaining data integrity and logical consistency. - **Use Case**: - Assigning roles to users where overlapping responsibilities are invalid (e.g., primary and secondary roles in a project). - Preventing duplicate selections in multi-step forms or hierarchical data inputs. **Visual Examples**  ## Key Code Segments ### Forms The form uses `TomSelectModelChoiceField` for the "Primary Author" and `TomSelectModelMultipleChoiceField` for "Contributing Authors". The `exclude_by` parameter ensures contributing authors exclude the selected primary author. :::{admonition} Form Definition :class: dropdown ```python class ExcludeByPrimaryAuthorForm(forms.Form): """Form with dependent fields demonstrating exclude_by functionality.""" primary_author = TomSelectModelChoiceField( config=TomSelectConfig( url="autocomplete-author", value_field="id", label_field="name", css_framework="bootstrap5", ), ) contributing_authors = TomSelectModelMultipleChoiceField( config=TomSelectConfig( url="autocomplete-author", value_field="id", label_field="name", exclude_by=("primary_author", "id"), css_framework="bootstrap5", placeholder=_("Select contributing authors..."), highlight=True, max_items=None, plugin_remove_button=PluginRemoveButton(), ), attrs={"class": "form-control mb-3"}, required=False, ) ``` ::: **Explanation**: - The `exclude_by` parameter in the `contributing_authors` field ensures that any selected "Primary Author" is removed from the available options in the "Contributing Authors" field. - The `RemoveButton` plugin improves user interaction by enabling quick removal of selected contributing authors. ### Templates The form is rendered in the `exclude_by.html` template, highlighting the exclusion mechanism between the two fields. :::{admonition} Template Code :class: dropdown ```html {% extends 'example/base_with_bootstrap5.html' %} {% block extra_header %} {{ form.media }} {% endblock %} {% block content %}