About Hiding The Clearing Button for Multiple-selection Autocomplete Fields

To hide the clearing button (✕) at multiple-selection autocomplete fields with django-autocomplete-light, set the "data-allow-clear" to "false", for example:

from django import forms
from dal import autocomplete
from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ["title", "content", "tags"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["tags"] = forms.ModelMultipleChoiceField(
            label=_("Tags"),
            queryset=Tag.objects.all(),
            required=False,
            to_field_name="slug",
            widget=autocomplete.ModelSelect2Multiple(
                url="tag-autocomplete",
                attrs={
                    "data-allow-clear": "false",
                },
            ),
        )

Tips and Tricks Programming Development Django 5.2 Django 4.2 Django 3.2 django-autocomplete-light

Django/Python Consulting

If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.