Filters in Search

15 views
Skip to first unread message

dtdave

unread,
Oct 20, 2016, 10:21:06 AM10/20/16
to Django users
I have an issue that I have got myself into a mess trying to solve, so any help would be appreciated.

I have implemented a simple search solution that works fine and this is my view:

from django.shortcuts import render
from django.views.generic import ListView
from jobs.models import Job
from django.db.models import Q
from .filters import JobFilter


class JobListView(ListView):
    model = Job
    paginate_by = 10
    template_name = 'jobs/job_list.html'

    def get_queryset(self):
        queryset = Job.objects.all()
        q = self.request.GET.get("q")
        if q:
            queryset = queryset.filter(
                Q(title__icontains=q) |
                Q(category__category__icontains=q) |
                Q(description__icontains=q)
            ).distinct()
        self.sort_by = self.request.GET.get('sort_by', 'start_date')
        return queryset.order_by(self.sort_by)

    def get_context_data(self,  **kwargs):
        context = super(JobListView, self).get_context_data(**kwargs)
        context['title'] = "Job List"
        context['sort_by'] = self.sort_by
        context['object_name'] = "job_list"
        return context

Everything thing works fine with this. However what I would like to do is to allow a person to filter on certain criteria within this page. I have created a separate view using django-filters which achieves this but would like to combine this. Any suggestions?
Thanks in advance. 
Reply all
Reply to author
Forward
0 new messages