Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete field in Django

I have models similar to the following:

class Country(models.Model):    
        name = models.CharField(max_length=50, unique=True)

class City(models.Model):
        name = models.CharField(max_length=50, unique=True)
        country = models.ForeignKey(Country)

and essentially I want to add a City to the database in my template. Before that, i should link it to the Country that already exists, so i want to use 'autocomplete field' in my template to get Country from DB

I have the following form defined:

class AddCityForm(forms.ModelForm):
    city_name = forms.CharField(max_length=100)
    country_name = forms.CharField(max_length=100)

In my template i have the forms like this :

<form action="/city/add" method="post">{% csrf_token %}
{{ add_city_form.as_p }}
<input type="submit" value="Submit" />
</form>

so is there any solution in django, to make the field 'country_name' autocompleted from the databese ?

cheers, newbie in django.

like image 936
Drwhite Avatar asked Apr 02 '13 17:04

Drwhite


2 Answers

As time passes, Django applications rise and fall. There's a grid on djangopackages.org that compares various autocompletion solutions and provides hints on their development status: https://djangopackages.org/grids/g/auto-complete/

like image 93
funky-future Avatar answered Oct 20 '22 11:10

funky-future


Yes. You can check django-autocomplete out

like image 24
karthikr Avatar answered Oct 20 '22 10:10

karthikr