Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add NumberInFilter for django_filters #98

Draft
wants to merge 1 commit into
base: numberinfilter
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/django_twc_toolbox/filters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
from __future__ import annotations

from django.core.exceptions import ImproperlyConfigured
from django.db.models import Q
from django_filters import BaseInFilter
from django_filters import Filter
from django_filters import NumberFilter


class NumberInFilter(BaseInFilter, NumberFilter):
pass


class SearchFilter(Filter):
def __init__(self, *args, search_fields=None, **kwargs):
if search_fields is None:
raise ImproperlyConfigured("The 'search_fields' argument is required.")
self.search_fields = search_fields
super().__init__(*args, **kwargs)

def filter(self, qs, value):
if not value:
return qs

value = value.strip().split(" ")

q = Q()
for field in self.search_fields:
for term in value:
q |= Q(**{field + "__icontains": term})
return qs.filter(q)
Loading