如何在Django管理员中实施依赖下拉列表?

时间:2019-03-14 18:35:27

标签: django django-admin django-2.1

我有多个相互依赖的下拉列表,如下所示: 省->城市->巴赫什->德黑斯坦->阿巴迪

我的模型是:

from django.db import models

# Create your models here.
class Province(models.Model):
    id = models.CharField(primary_key=True, max_length=2)

    name = models.CharField(max_length=50)
    family = models.IntegerField()
    population = models.IntegerField()
    men = models.IntegerField()
    women = models.IntegerField()

    def __str__(self):
        return self.name


class City(models.Model):
    id = models.CharField(primary_key=True, max_length=4)

    province = models.ForeignKey(Province, on_delete=models.CASCADE)

    name = models.CharField(max_length=60)
    family = models.IntegerField()
    population = models.IntegerField()
    men = models.IntegerField()
    women = models.IntegerField()

    def __str__(self):
        return self.name


class Bakhsh(models.Model):
    id = models.CharField(primary_key=True, max_length=6)

    province = models.ForeignKey(Province, on_delete=models.CASCADE)
    city = models.ForeignKey(City, on_delete=models.CASCADE)

    name = models.CharField(max_length=70)
    family = models.IntegerField()
    population = models.IntegerField()
    men = models.IntegerField()
    women = models.IntegerField()

    def __str__(self):
        return self.name


class Dehestan(models.Model):
    id = models.CharField(primary_key=True, max_length=10)

    province = models.ForeignKey(Province, on_delete=models.CASCADE)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    bakhsh = models.ForeignKey(Bakhsh, on_delete=models.CASCADE)

    name = models.CharField(max_length=80)
    family = models.IntegerField()
    population = models.IntegerField()
    men = models.IntegerField()
    women = models.IntegerField()

    def __str__(self):
        return self.name


class Abadi(models.Model):
    id = models.CharField(primary_key=True, max_length=16)

    province = models.ForeignKey(Province, on_delete=models.CASCADE)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    bakhsh = models.ForeignKey(Bakhsh, on_delete=models.CASCADE)
    dehestan = models.ForeignKey(Dehestan, on_delete=models.CASCADE)

    name = models.CharField(max_length=90)
    family = models.IntegerField(null=True)
    population = models.IntegerField(null=True)
    men = models.IntegerField(null=True)
    women = models.IntegerField(null=True)

    def __str__(self):
        return self.name

而我的admin.py文件是:

from django.contrib import admin
from .models import Province,City,Bakhsh,Dehestan,Abadi,License
# Register your models here.

admin.site.register(Province)
admin.site.register(City)
admin.site.register(Bakhsh)
admin.site.register(Dehestan)
admin.site.register(Abadi)

但是它不能正常工作。

我希望当我选择省时,出现相关的城市,然后出现相关的bakhsh,然后出现相关的dehestan,最后出现相关的abadi。

您有解决此问题的想法吗?

0 个答案:

没有答案