django-filter:为带有 LinkWidget 的 AllValuesFilter 添加 "All" 过滤器选项
django-filter: add "All" filter option for AllValuesFilter with LinkWidget
在多对多字段上使用 AllValuesFilter
过滤 django-filter 并通过 LinkWidget
显示,我没有得到 All
过滤选项,与 django 管理员一样 list_filter
.
我发现了 Add Any & None Handling to ChoiceFilter (and subclasses) 和其他各种相关问题,但我没有找到解决方案...
在这种特定情况下,我过滤 Django Wagtail Page
模型的类别:
# models.py
# wagtail imports
# ...
# modelcluster imports, eg.
# from modelcluster.fields import ParentalKey, ParentalManyToManyField
class CategoryRegion(models.Model):
title = models.CharField(max_length=200,)
class NewsPage(Page):
categories_region = ParentalManyToManyField(
"core.CategoryRegion",
blank=True,
)
--
# filters.py
import django_filters
from django_filters.widgets import LinkWidget
class NewsFilter(django_filters.FilterSet):
categories_region = django_filters.AllValuesFilter(
name="categories_region__title",
label="Categories: Region",
widget=LinkWidget(),
)
class Meta:
model = NewsPage
fields = ['categories_region',]
有什么提示吗?
bug where AllValuesFilter
does not have an all/any option. #680 应该可以解决该问题,但目前尚未合并。同时,您应该能够为 PR 安装相关分支,或者创建一个添加 "All" 选项的自定义子类。类似于:
class ActuallyAllValuesFilter(django_filters.AllValuesFilter):
@property
def field(self):
f = super(ActuallyAllValuesFilter, self).field
f.choices = [('', 'All')] + f.choices
return f
在多对多字段上使用 AllValuesFilter
过滤 django-filter 并通过 LinkWidget
显示,我没有得到 All
过滤选项,与 django 管理员一样 list_filter
.
我发现了 Add Any & None Handling to ChoiceFilter (and subclasses) 和其他各种相关问题,但我没有找到解决方案...
在这种特定情况下,我过滤 Django Wagtail Page
模型的类别:
# models.py
# wagtail imports
# ...
# modelcluster imports, eg.
# from modelcluster.fields import ParentalKey, ParentalManyToManyField
class CategoryRegion(models.Model):
title = models.CharField(max_length=200,)
class NewsPage(Page):
categories_region = ParentalManyToManyField(
"core.CategoryRegion",
blank=True,
)
--
# filters.py
import django_filters
from django_filters.widgets import LinkWidget
class NewsFilter(django_filters.FilterSet):
categories_region = django_filters.AllValuesFilter(
name="categories_region__title",
label="Categories: Region",
widget=LinkWidget(),
)
class Meta:
model = NewsPage
fields = ['categories_region',]
有什么提示吗?
bug where AllValuesFilter
does not have an all/any option. #680 应该可以解决该问题,但目前尚未合并。同时,您应该能够为 PR 安装相关分支,或者创建一个添加 "All" 选项的自定义子类。类似于:
class ActuallyAllValuesFilter(django_filters.AllValuesFilter):
@property
def field(self):
f = super(ActuallyAllValuesFilter, self).field
f.choices = [('', 'All')] + f.choices
return f