[Django]-Django Model Choice Field: Huge List of choices

2👍

Apparently, I had to override the Choice widget and change its render method to display no options. Since choices argument is still provided, the validation error does not occur and I can freely use javascript to render institutes by the city selected.

2👍

I had the same issue, but overcomed it by setting widget.choices to []

class StudentForm(forms.ModelForm):
    city = forms.ModelChoiceField(City)
    institute = forms.ModelChoiceField(queryset=Institute.objects.all())

def __init__(self, *args, **kwargs):
    super(StudentForm, self).__init__(*args, **kwargs)
    self.fields['institute'].widget.choices = []
👤soyacz

0👍

You should ensure that the Institute values that get filled up in the select box should also have the primary key as the html id attribute.

👤lprsd

Leave a comment