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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.x Django 4.2 Django 3.2 django-autocomplete-light