I have a form where I want to be able to let people choose a country, region or city using 1 form field (so they only have to choose 1). I am using django-autocomplete-light, so my plan is to combine all 3 model fields into 1 form field - the user searches and selects the one they want and it gets saved to the relevant model field.
Done a lot of searching, but can't find anything on this - any thoughts?
Here is my form currently, with each field separate:
class PostForm(forms.ModelForm):
country = forms.ModelChoiceField(Country.objects.all(),
widget=autocomplete_light.ChoiceWidget('CountryAutocomplete',
attrs={
'minimum_characters': 1,
}))
region = forms.ModelChoiceField(Region.objects.all(),
widget=autocomplete_light.ChoiceWidget('RegionAutocomplete',
attrs={
'minimum_characters': 1,
}))
city = forms.ModelChoiceField(City.objects.all(),
widget=autocomplete_light.ChoiceWidget('CityAutocomplete',
attrs={
'minimum_characters': 1,
}))
class Meta:
model = Post
exclude = ('user', 'active',)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
'',
'post',
'link',
'country',
'region',
'city',
),
ButtonHolder(
Submit('submit', 'Submit', css_class='btn btn-success')
)
)
super(PostForm, self).__init__(*args, **kwargs)
as you may see, I'm using django-autocomplete-light and django-crispy-forms
Not sure where to start on this one, so guidance and examples would be fantastic!!