Guest007
10/7/2016 - 4:28 AM

"OR" type of filter

"OR" type of filter

# got from https://github.com/carltongibson/django-filter/issues/137 (closed issue)
# I used variant from https://github.com/ddevlin - https://github.com/carltongibson/django-filter/issues/137#issuecomment-38158832
# like this: /?categ_in=3,4

from django_filters import Filter
from django_filters.fields import Lookup
from waypoints.models import WayPoint

class ListFilter(Filter):
    def filter(self, qs, value):
        value_list = value.split(u',')
        return super(ListFilter, self).filter(qs, Lookup(value_list, 'in'))

class ServiceFilter(filters.FilterSet):
    """
    categ - ordinary AllInclusive filter by name
    categ_in - filter only by id, but use OR logic: /?categ_in=3,4
    """
    categ = filters.AllLookupsFilter(name='services__name')
    categ_in = ListFilter(name='services__id')

    class Meta:
        model = WayPoint
        queryset = WayPoint.objects.all()
        fields = ['categ', 'categ_in']