smart select forms
Date: April 19th 2016
Last updated: April 19th 2016
Smart_selects is an app that provides filtering inside forms. The example used in the documentation shows a selection of continent filters the following possible selections of country which then filters further possible selections.
install (command line)
sudo pip3 install django-smart_selects
update settings.py and urls.py
# settings.py
INSTALLED_APPS = [
# <- snipped ->
'smart_selects',
]
# urls.py (project level - NOT app urls.py)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# <- snipped ->
url(r'^chaining/', include('smart_selects.urls')),
]
models.py
from smart_selects.db_fields import ChainedForeignKey
class Country(models.Model):
common_name = models.CharField('Common name', max_length=200)
#<- snipped ->
class Province(models.Model):
country = models.ForeignKey(Country)
#<- snipped ->
name = models.CharField('Province/State', max_length=255, unique=True)
class NearestCity(models.Model):
country = models.ForeignKey(Country)
province = models.ForeignKey(Province)
#<- snipped ->
name = models.CharField('Nearest City', max_length=255, unique=True)
# SMART SELECTS IN BEACH MODEL
class Beach(models.Model):
country = models.ForeignKey(Country)
# link province to country
# country must be entered to filter this list
province = ChainedForeignKey(
Province,
chained_field="country",
chained_model_field="country",
show_all=False,
auto_choose=True
)
# link city to province
# country and province must be selected to filter this list
city = ChainedForeignKey(
NearestCity,
chained_field="province",
chained_model_field="province",
show_all=False,
auto_choose=True
)
beachname = models.CharField('Beach name', max_length=255)