Reputation: 1853
I'm trying to add a filter feature in my django-tables 2
so I opted for django-filter
. I followed the documentation it worked great but I don't know how to integrate it with django-tables 2
.
I made a filter class and in the view i made something like this :
queryset = Fitzroyfalls.objects.select_related().all()
f = FitzroyfallsFilter(request.GET, queryset=queryset)
table = FitsroyFallsTable(f.queryset)
table.paginate(page=request.GET.get('page', 1), per_page=25)
RequestConfig(request).configure(table)
return render(request, 'query.html', {'table': table})
but nothing happens, it only displays the table with all data.
Upvotes: 2
Views: 6519
Reputation: 17095
For CBV:
class FilteredSingleTableView(FilterView, SingleTableView):
def get_table_data(self):
data = super(FilteredSingleTableView, self).get_table_data()
return data if self.object_list is None else self.object_list
Usage:
class UserList(FilteredSingleTableView):
template_name = 'accounts/users_list.html'
model = User
table_class = UserTable
paginate_by = 10
filterset_class = UserFilter
Upvotes: -1
Reputation: 3881
I checked out the __iter__
method for the FilterSet
and it basically accesses the property
qs
. So @huiming's answer works for me. I adapted your code as follows:
queryset = Fitzroyfalls.objects.select_related().all()
f = FitzroyfallsFilter(request.GET, queryset=queryset)
table = FitsroyFallsTable(f.qs)
RequestConfig(request, paginate={"per_page": 25, "page": 1}).configure(table)
return render(request, 'query.html', {'table': table, 'filter': f})
I'm using:
django-filter==0.7
django-tables2==0.14.0
Django==1.6
Upvotes: 3
Reputation: 1853
After hours of trying, I got the solution which is simple. In the view add those lines :
if request.method == "GET"
list1=list()
for obj in f:
list1.append(obj)
table=FitsroyFallsTable(list1)
aaand that's it !
Upvotes: 1