How do i manage to use a key-selection in a form using the autocomplete_light.MultipleChoiceWidget ?
I would like to select entries of a m2m field by using the comma key intead of choosing it via mouseclick. I found the related paragraph in the docs but i don't understand how to construct it.
models.py
class MyModel(models.Model):
tag = models.ManyToManyField(Tag, blank=True, null=True)
class Tag(models.Model):
name = models.CharField(max_length=32, unique=True)
autocomplete_light_registry.py
autocomplete_light.register(Tag,
search_fields=['name'],
autocomplete_js_attributes={
'placeholder': 'Insert additional tags',
},
widget_js_attributes = {
'max_values': 3,
}
)
forms.py
tag = forms.ModelMultipleChoiceField(Tag.objects.all(),
widget=autocomplete_light.MultipleChoiceWidget('TagAutocomplete'),
required=False)
The autocompletion works fine except that you have to click on the desired tag. I'm looking for a solution how to select an entry using a key like spacebar or comma.
If you want entries separated by the comma caracter then you need to use an autocomplete_light.TextWidget
. However, it doesn't work for relations, it will work only for CharField
and the like.
MultipleChoiceWidget
maintains a sane hidden select element. The select needs to contain <option>
which have value=the_foreign_key
. If this select was maintained by an input with a comma separated list of object titles, then a user could checkmate it by modifying a previously entered object name - not to mention that an object name change at the time of selection would checkmate the autocomplete as well.