嘗試ChainedForeignKey從django-smart-selects包它的作品就像一個魔術。 smart_selects的
安裝
pip install django-smart-selects
項目settings.py
INSTALLED_APPS=[
. . . . .
. . . . .
'smart_selects',
. . . . .
]
項目urls.py
urlpatterns = [
. . . . .
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
. . . . .
]
見installation of django-smart-selects
工作ChainedForeignKey
的
from smart_selects.db_fields import *
class State(models.Model):
stateName = models.CharField(max_length=500)
. . . .
class Region(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
regionName = models.CharField(max_length=500)
. . . . .
. . . . .
class Cluster(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region,chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
clusterName = models.CharField(max_length=500)
. . . . .
. . . . .
class school(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
schoolName = models.CharField(max_length=500)
. . . .
. . . .
試試這個,它運作良好,在admin area
。
希望它有幫助!!!!!
有趣的問題。我想知道如果這是可能的。 –
這真的會幫助管理員管理他們網站上的東西。讓我們希望有人會提出一個解決方案。 –
@PabloVergés,我已經回答了這個問題,希望它能幫助你! –