I'm using Django Autocomplete Light widget Select2Multiple for a form field for tags. In form.clean()
, the corresponding value is a string of list of the pk's of the tags, which looks like this:
form.clean()['tags']: "['1','2']"
Now, I can convert this to a list of integers and process the pk's individually but I feel like there should be a more obvious way to handle this.
Relevant field in forms.py:
tags = CharField(label='Tags',
max_length=50,
required=False,
widget=autocomplete.Select2Multiple(url='tag-autocomplete')
)
PS: I can't use ModelSelect2Multiple
as this form is for an object that hasn't yet ben created.
forms.CharField
is not designed to handle a list of values. Since you are selecting model instances, you can use forms.ModelMultipleChoiceField
:
tags = forms.ModelMultipleChoiceField(label='Tags',
queryset=Tag.objects.all(),
max_length=50,
required=False,
widget=autocomplete.Select2Multiple(url='tag-autocomplete')
)