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

问题描述 投票:0回答:1

我想基于父级连击的选择来填充从属连击。我的代码如下:

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导入管理员

来自.models import物种,SpeciesDetail,对,

在这里注册您的模型。

class PairAdmin(admin.ModelAdmin):
    list_display = ('species', 'male', 'female',)
    search_fields = ('male', 'female', )
    filter_horizontal = ()
    list_filter = ()
    ordering = ['species']
    fieldsets = ()
django chaining
1个回答
0
投票

答案是DAL(Django Autocomplete Light。

© www.soinside.com 2019 - 2024. All rights reserved.