如何填充(链式)依赖下拉列表

How to populate (Chained) Dependent Dropdown

我想根据父组合的选择来填充依赖组合。我的代码如下:

class Species(models.Model):
    name = models.CharField(max_length=120, blank=False, null=False)
    description = models.TextField(max_length=300, blank=False, null=False)

class SpeciesDetail(models.Model):
    species = models.ForeignKey(Species, on_delete=models.CASCADE)
    picture = models.ImageField(upload_to='pics')
    gender = models.CharField(max_length=1)
    health = models.CharField(max_length=120)


class Pair(models.Model):
    species = models.ForeignKey(Species, on_delete=models.CASCADE)
    male = models.ForeignKey(SpeciesDetail, on_delete=models.CASCADE, related_name='male_set')
    female = models.ForeignKey(SpeciesDetail, on_delete=models.CASCADE, related_name='female_set')

从django.contrib导入管理员

from .models import Species, SpeciesDetail, Pair,

在此处注册您的模型。

class PairAdmin(admin.ModelAdmin):
    list_display = ('species', 'male', 'female',)
    search_fields = ('male', 'female', )
    filter_horizontal = ()
    list_filter = ()
    ordering = ['species']
    fieldsets = ()

答案是 DAL(Django Autocomplete Light。