user1159006
user1159006

Reputation: 1

dynamic combobox in django admin

I'm having the following problem, I have five classes: Continent, Country, State, City and Customer, the Customer class would like a list of countries would be charged based on the selected continent, as well as states according to the selected country, and the cities according with the selected state. All this in ADMIN. I would like to help a as an example.

# models.py:

class Continent(models.Model):
    name_co = models.CharField(max_length=50, unique=True, verbose_name='continent')

class Country(models.Model):
    name_cy = models.CharField(max_length=50, unique=True, verbose_name=u'country')
    continent_cy = models.ForeignKey(Continent, verbose_name=u'continent')

class State(models.Model):
    name_st = models.CharField(max_length=50, verbose_name=u'state')
    country_st = models.ForeignKey(Country, verbose_name=u'country')

class City(models.Model): 
    name_ci = models.CharField(max_length=50, verbose_name=u'city')
    state_ci = models.ForeignKey(State, verbose_name=u'state') 

class Customer(models.Model):
    name_cu = models.CharField(max_length=50, verbose_name=u'name')   
    continent_cu = models.ForeignKey(Continent, verbose_name='continent') 
    country_cu = models.ForeignKey(Country, verbose_name=u'country') 
    state_cu = models.ForeignKey(State, verbose_name=u'state') 
    city_cu = models.ForeignKey(City, verbose_name=u'city') 

Upvotes: 0

Views: 2132

Answers (2)

dan-klasson
dan-klasson

Reputation: 14190

Django Smart Selects will most likely do that for you. From the README:

If you have the following model:

class Location(models.Model)
    continent = models.ForeignKey(Continent)
    country = models.ForeignKey(Country)

And you want that all countries are grouped by the Continent and that Groups are used in the select change to the following:

from smart_selects.db_fields import GroupedForeignKey

class Location(models.Model)
    continent = models.ForeignKey(Continent)
    country = GroupedForeignKey(Country, "continent")

Upvotes: 4

Diego Navarro
Diego Navarro

Reputation: 9704

I think there is no way to achieve this directly with Django. Try with AJAX and jQuery.

Upvotes: 1

Related Questions