Django-filter won't display TreeNodeChoiceField (MPTT package) as tree choice field

14 Views Asked by At

I have a Category MPTT model. I have a Transaction django model which has a ForeignKey assigned to Category model (field = 'category'). Using django-filter lib I'm trying to filter Transactions in a ListView. Everything works fine except the 'category' filter field appears as choice field with plain list of all categories instead of list with tree 'leveling'.

My FilterSet class:

class TransactionsListFilter(django_filters.FilterSet):
    """ Filters for Transaction list. """

    title = django_filters.CharFilter(lookup_expr="icontains", label="Transaction title")
    date_created = django_filters.DateFromToRangeFilter(widget=RangeWidget(attrs={'type': 'date'}))
    category = TreeNodeChoiceField(queryset=Category.objects.all())

    class Meta:
        model = Transaction
        fields = ["title", "category", "operation", "date_created"]

I was trying to add a TreeNodeChoiceField as I did the same in forms (there it works) but in FilterSet class don't make a trick. The dropdown choice field is still plain list without category leveling by subcategory. Is there a way to make the choice list appears with as tree?

0

There are 0 best solutions below