I'm trying to add autocomplete to a country field on out model. The autocomplete is populating when this code is added:

# form.py
class CustomUserChangeForm(UserChangeForm):
    country = autocomplete.Select2ListCreateChoiceField(
        choice_list=get_user_model().get_country_choice_list,
        # widget=autocomplete.ListSelect2(url='http://127.0.0.1:8000/accounts/country-list-autocomplete',),
    )

    class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'company', 'country')

but when i decomment the widget line the field is rendered as empty in the dropdown, and it doesn't resemble an input at all- rather a dropdown still.

get_user_model().get_country_choice_list returns a list as expected: ['Afghanistan', 'Ă…land Islands', 'Albania', 'Algeria'...

My endpoint is functioning correctly (assuming the format is correct):

{
"results": [
    {
    "id": "Afghanistan",
    "text": "Afghanistan"
    },
    ...

for the widget i've tried the following options:

widget=autocomplete.ListSelect2(url='http://127.0.0.1:8000/accounts/country-list-autocomplete',),
widget=autocomplete.ListSelect2(url='accounts/country-list-autocomplete',),
widget=autocomplete.ListSelect2(url='country-list-autocomplete',),

each to no avail. I have no error in the terminal, and no request is generated.

The html element looks like this when the widget is uncommented:

<select name="country"
        id="id_country"
        data-autocomplete-light-language="en-US"
        data-autocomplete-light-url="/accounts/country-list-autocomplete"
        data-autocomplete-light-function="select2">
    <option value="Antarctica" selected="">Antarctica</option>
</select>

Any ideas why this is not working?

Edit- couple of notes on installation:

  • using django 2.2.1
  • jquery 3.3.1
  • I've installed in installed apps ebfore django.contrib.admin

0 Answers