django-datatable-view icon indicating copy to clipboard operation
django-datatable-view copied to clipboard

Using django-filters with this module

Open jangeador opened this issue 8 years ago • 1 comments

Has anyone been able to make this app work together with django-filter? I have tried but for some reason at the last minute my queryset gets overriden this is because all the parameters for the datatable get passed via the request.GET which is what django-filters use:

from datatableview import Datatable
from datatableview.views import DatatableView
from django.db import models
from django_filters import FilterSet
from django_filters.views import FilterMixin


class Student(models.Model):
    school = models.CharField(max_length=255, blank=True, null=True)
    grade = models.CharField(max_length=255, blank=True, null=True)
    last_name = models.CharField(max_length=255, blank=True, null=True)
    first_name = models.CharField(max_length=255, blank=True, null=True)
    middle_name = models.CharField(max_length=255, blank=True, null=True)


class StudentFilter(FilterSet):
    class Meta:
        model = Student
        fields = ['school', 'grade']


class StudentDataTable(Datatable):
    class Meta:
        model = Student
        structure_template = 'datatableview/bootstrap_structure.html'
        columns = ['school', 'grade', 'last_name', 'first_name']


class StudentListView(FilterMixin, DatatableView):
    model = Student
    datatable_class = StudentDataTable
    template_name = 'afb/datatable.html'
    filter = None

    def get_queryset(self):
        qs = super(StudentListView, self).get_queryset()
        self.filter = StudentFilter(self.request.GET, queryset=qs)
        return self.filter.qs

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

jangeador avatar Oct 04 '17 19:10 jangeador

No promises on timeline yet, but this is on my todo list for a new release

tiliv avatar Oct 05 '18 23:10 tiliv