django-filter chained select



我正在使用django-filters lib https://django-filter.readthedocs.io/en/master/index.html。我需要在我的过滤器中制作链式选择下拉列表。我知道如何用简单的django形式来制作它,就像 https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html 这里一样。当用户选择区域时,我需要显示该区域的城市吗?有人知道如何构建这样的过滤器或解决方案吗?

将 django-smart-selects 与你执行过滤的方式集成在一起。

此包允许您通过向模型添加自定义外键或多对多字段来快速筛选或分组"链接"模型。这将使用 AJAX 查询仅加载适用的链式对象。

Region -> City的原始问题类似,文档的示例Continent -> Country完全符合需要。

选择大洲后,如果希望仅该大陆上的国家/地区可用,则可以在位置模型上使用ChainedForeignKey

class Location(models.Model):
    continent = models.ForeignKey(Continent)
    country = ChainedForeignKey(
        Country,
        chained_field="continent",  # Location.continent
        chained_model_field="continent",  # Country.continent
        show_all=False,  # only the filtered results should be shown
        auto_choose=True,
        sort=True)

相关问题:

  • 如何使用 django-smart-select

最新更新