I need to have a form where user enters name, email and selects a city and country. I am using django-cities-light to populate the database with list of cities and countries.
Then, I create a form using django-crispy-forms using the database model as shown below.
Models.py
from cities_light.models import City, Country
class CustomModel(models.Model):
name = models.CharField(max_length=500)
email = models.EmailField(blank=True)
city = models.ForeignKey(City)
country = models.ForeignKey(Country)
Forms.py
Class EntryForm(forms.ModelForm):
class Meta:
model = CustomModel
def __init__(self, *args, **kwargs):
super(EntryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout.append(ButtonHolder(
Submit('save', 'Save', css_class='btn-primary')
)
)
Using this in a view gives me a form with two text fields (for name, email) and two select fields (for city, country). Now, since the number of cities is pretty high, I wish to change these to text fields that use django-ajax-selects to show names of cities or countries when user starts typing.
A section of documentation provides AJAX_LOOKUP_CHANNELS, but I can't quite understand how to change my form and then connect it with ajax-selects. Any help is highly appreciated.