The LinkWidget will list the filtered results as an unordered list and list items in html.
Ie:
<ul id="id_categories">
<li><a href="?categories=">All</a></li>
<li><a class="selected" href="?categories=1">Offices</a></li>
<li><a href="?categories=2"> Residential </a></li>
</ul>
To achieve this you can use “django_filters.filters.ModelChoiceFilter”. Despite that the field to be filtered is a ManyToManyField.
According to the documentation:
ModelMultipleChoiceFilter?
Similar to a MultipleChoiceFilter except it works with related models, used for ManyToManyFieldby default.
ModelChoiceFilter?
Similar to a ChoiceFilter except it works with related models, used for ForeignKey by default.
#filters.py
class ProjectFilter(django_filters.FilterSet):
categories = django_filters.filters.ModelChoiceFilter(
required=False,
queryset=Category.objects.all(),
widget=django_filters.widgets.LinkWidget
)
class Meta:
model = Project
fields = ['categories' ]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…