In my filters.py
:
class DataFilter(FilterSet):
start_date = DateFilter(field_name='date',lookup_expr=('lt'))
end_date = DateFilter(field_name='date',lookup_expr=('gt'))
date_range = DateRangeFilter(name='date')
class Meta:
model = DataModel
fields = ['date', ]
I have also tried setting fields = []
, but filters of all fields are still there.
Why is it showing all even I just only specified one (even none)? And how to fix that?
Can anyone help explain? Thank you!
In my models.py
:
class DataModel(models.Model):
date = models.DateField(default=now)
other_field_1 = models.CharField()
other_field_2 = models.CharField()
other_field_3 = models.CharField()
In my views.py
:
class DataModelListView(LoginRequiredMixin, FilterView):
model = DataModel
template_name = 'datamodel_list.html'
filter_class = DataFilter
I am using django 3, django-filter 21.
Solution:
Since modifying FilterSet.Meta
doesn't work anyway, I then tried looking at other parts of the code. Then I found changing filter_class
to filterset_class
in the FilterView
(as below) work for me!
class DataModelListView(LoginRequiredMixin, FilterView):
filterset_class = DataFilter # instead of setting filter_class
template_name = 'datamodel_list.html'
# model = DataModel # Either filterset_class or model needs to be set.