Your problem is not specific to django-autocomplete-light. What you're doing has no chance to work and here's why:
- the form field for a
ForeignKey
likelocation
is a ModelChoiceField by default, ModelChoiceField
accepts values which are pks of models inModelChoiceField.queryset
, which isTheModel.objects.all()
by default,- the
TextWidget
widget is aTextInput
, - a
TextInput
widget is just an<input type="text" />
, - the value of a
<input type="text" />
is directly sent to the server on form submission.
As a result, selecting a couple of cities like "Lisboa" and "Madrid" with a text widget will look like::
<input type="text" value="Lisboa, Madrid" name="location" />
Which means that the form will post {'location': 'Lisboa, Madrid'}
. While this is good for a CharField
, it won't work for a ModelMultipleChoiceField
which would expect something like {'location': [3,5]}
where 3 would be the pk of Lisboa and 5 the pk of Madrid.
In the same fashion, a ModelChoiceField
would expect {'location': 3}
which autocomplete_light.ChoiceWidget
is able to do.
To fix this, use a ChoiceWidget
instead of a TextWidget
. I have clarified this in the tutorial I hope it is better now.