How can I update the ‘City’ dropdown based on the selected ‘Country’? Is there anything built on Django to facilitate this?
class Country(models.Model):
name = models.CharField(max_length=50)
...
class City(models.Model):
name = models.CharField(max_length=50)
country = models.ForeignKey(Country)
class UserProfile(models.Model):
...
country = models.ForeignKey(Country, default=1)
city = models.ForeignKey(City, default=1)
user = models.OneToOneField(User)
And in my view I have:
{{ profile_form.country }}
{{ profile_form.city }}
But all cities are shown at once.
Please advise.
My answer would be a bit long, so i suggest you can have a look at this implementation, which answers exactly what you are looking for.