Empty result list on django-filter page startup

后端 未结 1 1168
-上瘾入骨i
-上瘾入骨i 2021-01-20 23:57

I\'m using https://github.com/alex/django-filter

When user opens the page with filter at the first time, they see the empty form and full list of results.

I need

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 23:59

    After some studies, I've found this simple solution

    class ProductFilter(django_filters.FilterSet):
        class Meta:
            model = Product
            fields = [ 'shortname', 'fullname', 'description', 'product_type',   ]
    
        def __init__(self, *args, **kwargs):
            super(ProductFilter, self).__init__(*args, **kwargs)
            # at sturtup user doen't push Submit button, and QueryDict (in data) is empty
            if self.data == {}:
                self.queryset = self.queryset.none()
    

    I think, this solution is stable. How do you think?

    0 讨论(0)
提交回复
热议问题