'Django Form field for selecting multiple options from a long list
In my user model I have a ManyToMany field to a languages table, which stores the languages a user speaks:
spokenLanguages = models.ManyToManyField(Languages, through='LanguageKnowledge', blank=True)
I would now like to create a form for the user to select multiple languages from those options.
When using a ModelForm the default form field for a ManyToMany model field is a MultipleChoiceField which has SelectMultiple as its default widget.
When working with a large number of choices however, this form field is not useful, as one has to browse a long list in order to find a desired choice. Also selecting multiple options is only possible trough shift-clicking the entries. Overall this makes for terrible UX; this is what it looks like:
Instead I would like to have something like a CharField that opens a dropdown menu of Choices once focused. These options should then be refined depending on what the user types (e.g. if they type "Be" the dropdown should only show Belarusian and Bengali).
Furthermore once the user chooses one of the options it should be added to a list of selected laguages that are visible to the user, from which the choices should also be able to be removed again.
All in all the solution could look something like the tag chooser used on the Stackexchange sites when writing a question:
Does anyone know of a package that could achieve somwthing similar (I couln't find anything)?
Otherwise, what would be the best way to go about creating such a form field? Which field should I subclass from? Which Widget(s) could I use? Do I need to worry about the DB integration?
Is it even wise to do all this in the backend or is there a simpler frontend solution?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


