AlexW
AlexW

Reputation: 2587

django-tables2/django-filter - pagination with filterclass

Good Afternoon,

I am using a custom django-filter in django-tables2 to search all fields with a single input. Ive just noticed that when I search I lose my pagination menu.

this is the link to the filter code https://spapas.github.io/2016/09/12/django-split-query/

here's my filter

class SiteFilterEx(django_filters.FilterSet):
    ex = django_filters.CharFilter(label='Ex filter', method='filter_ex')
    search_fields = ['location', 'bgp_as', 'opening_date','town','postcode']

    def filter_ex(self, qs, name, value):
        if value:
            q_parts = value.split()

            # Use a global q_totals
            q_totals = Q()

            # This part will get us all possible segmantiation of the query parts and put it in the possibilities list
            combinatorics = itertools.product([True, False], repeat=len(q_parts) - 1)
            possibilities = []
            for combination in combinatorics:
                i = 0
                one_such_combination = [q_parts[i]]
                for slab in combination:
                    i += 1
                    if not slab: # there is a join
                        one_such_combination[-1] += ' ' + q_parts[i]
                    else:
                        one_such_combination += [q_parts[i]]
                possibilities.append(one_such_combination)

            # Now, for all possiblities we'll append all the Q objects using OR
            for p in possibilities:
                list1=self.search_fields
                list2=p
                perms = [zip(x,list2) for x in itertools.permutations(list1,len(list2))]

                for perm in perms:
                    q_part = Q()
                    for p in perm:
                        q_part = q_part & Q(**{p[0]+'__icontains': p[1]})
                    q_totals = q_totals | q_part

            qs = qs.filter(q_totals)
        return qs    

    class Meta:
        model = Site
        fields = ['ex']     
        form = SiteFilterForm  

in my template I can use:

Showing {{ filter.qs.count }} of {{ filter.queryset.count }} records 

but the default Django-tables uses

Showing {{ table.page.start_index }} to {{ table.page.end_index }} of {{ table.page.paginator.count }} records 

and when I filter the pagination is gone completely.

I think I need to return a paginate with the same name some how? but am unsure of what I need to do?

here's the code from the view:

class Sites(LoginRequiredMixin, ExportMixin, SingleTableMixin, FilterView):
    model = Site
    table_class = SiteTable
    template_name = "app_settings/table_view.html"
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
    filterset_class = SiteFilterEx
    exclude_columns = ("buttons", )

    def dispatch(self, *args, **kwargs):
        site_type = get_object_or_404(SiteType, pk=self.kwargs['site_type'])
        site_state = 'Open' if self.kwargs['state'] else 'Closed'
        self.site_type_name = '{} {}s'.format(site_state, site_type.site_type)
        self.site_type_icon = 'fa {}'.format(site_type.icon)
        return super(Sites, self).dispatch(*args, **kwargs)

    def get_queryset(self):
        site_type = self.kwargs['site_type']
        subnet = Subquery(
            DeviceCircuitSubnets.objects.filter(device__site_id=OuterRef('id'), \
                                        active_link=True, \
                                        circuit__decommissioned=False
                                        ).values('circuit__name')[:1])        
        active_circuit = Subquery(
            DeviceCircuitSubnets.objects.filter(device__site_id=OuterRef('id'), \
                                        active_link=True, \
                                        circuit__decommissioned=False
                                        ).values('circuit__name')[:1])
        if site_type:
            return super(Sites, self).get_queryset().filter(
                is_live=self.kwargs['state'], site_type_id=site_type
            ).annotate(
                active_circuit=active_circuit
            ).prefetch_related('sitesupernet_set')
        else:
            return super(Sites, self).get_queryset().filter(
                is_live=self.kwargs['state']
            ).annotate(
                active_circuit=active_circuit
            ).prefetch_related('sitesupernet_set')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)     
        context['page_icon']=self.site_type_icon
        context['page_title']=self.site_type_name 
        return context  

Upvotes: 5

Views: 1665

Answers (1)

Raddish IoW
Raddish IoW

Reputation: 467

The following might help: https://kuttler.eu/en/post/using-django-tables2-filters-crispy-forms-together/

Notably these few lines:

As SingleTableView inherits from Django's ListView you automatically get a Paginator object in the template context as paginator.

The get_queryset() method was changed to apply the filter and to return the filtered queryset. That filtered data ends up in the table in get_table() and gets paged. Aftert that it is added to the template context together with the filter.

I used this in my project to display a filtered, paged table, using the following as a base class for my view: https://gist.github.com/craigderington/0a7ded018b8401bc42822b2eeaaff1cd

Like this:

class PagedFilteredTableView(SingleTableView):
    filter_class = None
    formhelper_class = None
    context_filter_name = 'filter'
    paginator_class = LazyPaginator

    def get_queryset(self, **kwargs):
        qs = super(PagedFilteredTableView, self).get_queryset()
        self.filter = self.filter_class(self.request.GET, queryset=qs)
        self.filter.form.helper = self.formhelper_class()
        return self.filter.qs

    def get_context_data(self, **kwargs):
        context = super(PagedFilteredTableView, self).get_context_data()
        context[self.context_filter_name] = self.filter
        return context

With my view class:

class JobList(LoginRequiredMixin, PermissionRequiredMixin, ExportMixin, PagedFilteredTableView):

    permission_required = 'jobs.view_job'
    template_name = 'jobs/job_list.html'
    model = Job
    context_object_name = 'job_list'
    table_class = JobTable
    table_pagination = {'per_page': 20}
    filter_class = JobListFilter
    formhelper_class = forms.JobListFilterFormHelper
    # Columns to exclude in the export
    exclude_columns = ('duration', 'job_type', 'contact_name', 'job_status',)

    def get_queryset(self):
        qs = super(JobList, self).get_queryset()
        ...
        self.filter.form.helper.add_input(
            Submit('submit', 'Filter',
                   css_class='btn pmd-ripple-effect btn-lg btn-primary btn-block',
                   style='margin-top:10px;')
        )
        return qs

    def get_context_data(self, **kwargs):
        context = super(JobList, self).get_context_data(**kwargs)
        search_query = self.get_queryset()
        table = JobTable(search_query)
        RequestConfig(self.request).configure(table)
        context['table'] = table
        return context

So, for your view, you could add the PagedFilteredTableView class to your views.py above your Sites class, then change your Sites class like this:

class PagedFilteredTableView(SingleTableView):
    filter_class = None
    formhelper_class = None
    context_filter_name = 'filter'
    paginator_class = LazyPaginator

    def get_queryset(self, **kwargs):
        qs = super(PagedFilteredTableView, self).get_queryset()
        self.filter = self.filter_class(self.request.GET, queryset=qs)
        self.filter.form.helper = self.formhelper_class()
        return self.filter.qs

    def get_context_data(self, **kwargs):
        context = super(PagedFilteredTableView, self).get_context_data()
        context[self.context_filter_name] = self.filter
        return context


class Sites(LoginRequiredMixin, ExportMixin, PagedFilteredTableView):
    model = Site
    table_class = SiteTable
    template_name = "app_settings/table_view.html"
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
    filter_class = SiteFilterEx
    exclude_columns = ("buttons", )
    table_pagination = {'per_page': 20}

    def dispatch(self, *args, **kwargs):
        site_type = get_object_or_404(SiteType, pk=self.kwargs['site_type'])
        site_state = 'Open' if self.kwargs['state'] else 'Closed'
        self.site_type_name = '{} {}s'.format(site_state, site_type.site_type)
        self.site_type_icon = 'fa {}'.format(site_type.icon)
        return super(Sites, self).dispatch(*args, **kwargs)

    def get_queryset(self):
        site_type = self.kwargs['site_type']
        subnet = Subquery(
            DeviceCircuitSubnets.objects.filter(device__site_id=OuterRef('id'), \
                                        active_link=True, \
                                        circuit__decommissioned=False
                                        ).values('circuit__name')[:1])        
        active_circuit = Subquery(
            DeviceCircuitSubnets.objects.filter(device__site_id=OuterRef('id'), \
                                        active_link=True, \
                                        circuit__decommissioned=False
                                        ).values('circuit__name')[:1])
        if site_type:
            return super(Sites, self).get_queryset().filter(
                is_live=self.kwargs['state'], site_type_id=site_type
            ).annotate(
                active_circuit=active_circuit
            ).prefetch_related('sitesupernet_set')
        else:
            return super(Sites, self).get_queryset().filter(
                is_live=self.kwargs['state']
            ).annotate(
                active_circuit=active_circuit
            ).prefetch_related('sitesupernet_set')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)     
        context['page_icon']=self.site_type_icon
        context['page_title']=self.site_type_name 
        return context

Upvotes: 1

Related Questions