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.x Django 4.2 Django 3.2 django-autocomplete-light
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.