【问题标题】:Django smart select many to many field filter_horizontal/filter_vertical does not allow chainingDjango 智能选择多对多字段 filter_horizontal/filter_vertical 不允许链接
【发布时间】:2016-11-13 14:14:12
【问题描述】:
您好,我正在 Django 1.10 中做一个项目。对于这个项目,我使用 django-smart-select 在管理面板中链接输入。
它工作正常。但是对于多对多字段链接,如果我使用 filter_horizontal/filter_vertical 则链接不再起作用。
github页面中没有解决方案。
我怎么解决这个问题?有没有其他类似的应用程序?
【问题讨论】:
标签:
python
django
many-to-many
chaining
django-smart-selects
【解决方案1】:
我有同样的问题,我在图书馆的这个 fork 中解决了它:https://github.com/jorgecorrea/django-smart-selects
正如您在我的 README 版本中看到的,这是在水平模式下使用 mi fork 的方式:
模型.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
不需要此更改:
管理员.py
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
filter_horizontal = ('writer',)
# don't do that because you will be changing the widget.