How to implement Depended Drop Down List in Django administrator?

Refresh

March 2019

Views

4 time

0

I have multiple dropdown lists that depended on each other as below: Province-->City-->Bakhsh-->Dehestan-->Abadi

my models are:

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

and my admin.py file is:

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)

But it doesn't work correctly.

I want that when I select the province, related cities appear then related bakhsh appear then related dehestan appear and finally related abadi appear.

Do you have any idea to solve this problem?

0 answers