我有同樣的問題,我解決它在從庫這個叉:https://github.com/jorgecorrea/django-smart-selects 正如你可以在我的自述版本看,這是有心肌梗死叉水平模式使用方式: models.py
from smart_selects.db_fields import ChainedManyToManyField
class Publication(models.Model):
name = models.CharField(max_length=255)
class Writer(models.Model):
name = models.CharField(max_length=255)
publications = models.ManyToManyField('Publication',
blank=True,
null=True)
class Book(models.Model):
publication = models.ForeignKey(Publication)
writer = ChainedManyToManyField(
Writer,
horizontal=True,
verbose_name='writer',
chained_field="publication",
chained_model_field="publications",
)
name = models.CharField(max_length=255)
有了這個,你可以使用Django水平模式的看法,但不添加現場管理filter_horizontal 不需要這種變化變化不大: admin.py
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
filter_horizontal = ('writer',)
# don't do that because you will be changing the widget.