diff --git a/config/settings.py b/config/settings.py index 8a8d2ad..a664e98 100644 --- a/config/settings.py +++ b/config/settings.py @@ -41,7 +41,9 @@ 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', 'django_extensions', + 'django_filters', 'custom_user.apps.CustomUserConfig', ] @@ -56,6 +58,10 @@ # Setting the permission policy # https://www.django-rest-framework.org/api-guide/permissions/#setting-the-permission-policy +# Integration with Django Rest Framework is provided through a DRF-specific FilterSet and a filter backend. +# These may be found in the rest_framework sub-package. +# https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html + REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', @@ -65,7 +71,11 @@ 'DEFAULT_PERMISSION_CLASSES': [ # 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.AllowAny', - ] + ], + + 'DEFAULT_FILTER_BACKENDS': ( + 'django_filters.rest_framework.DjangoFilterBackend', + ), } # Some of Simple JWT’s behavior can be customized through settings variables in settings.py diff --git a/custom_user/views.py b/custom_user/views.py index 1ede235..be8fc92 100644 --- a/custom_user/views.py +++ b/custom_user/views.py @@ -1,3 +1,4 @@ +from django_filters.rest_framework import DjangoFilterBackend from rest_framework import generics from custom_user.models import CustomUser from custom_user.serializers import CustomUserListSerializer, CustomUserCreateSerializer @@ -8,6 +9,8 @@ class CustomUserListView(generics.ListAPIView): serializer_class = CustomUserListSerializer queryset = CustomUser.objects.all() + filter_backends = [DjangoFilterBackend] + filterset_fields = ('is_seller', ) class CustomUserCreateView(generics.CreateAPIView): diff --git a/poetry.lock b/poetry.lock index e357920..7573c38 100644 --- a/poetry.lock +++ b/poetry.lock @@ -185,6 +185,21 @@ files = [ [package.dependencies] Django = ">=3.2" +[[package]] +name = "django-filter" +version = "23.3" +description = "Django-filter is a reusable Django application for allowing users to filter querysets dynamically." +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "django-filter-23.3.tar.gz", hash = "sha256:015fe155582e1805b40629344e4a6cf3cc40450827d294d040b4b8c1749a9fa6"}, + {file = "django_filter-23.3-py3-none-any.whl", hash = "sha256:65bc5d1d8f4fff3aaf74cb5da537b6620e9214fb4b3180f6c560776b1b6dccd0"}, +] + +[package.dependencies] +Django = ">=3.2" + [[package]] name = "djangorestframework" version = "3.14.0" @@ -815,4 +830,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "50d96cd6fa7d6f56a568c87fc0683e4a0d4d5e45714e4d6c1a4e249bc419e6cc" +content-hash = "acc571445b5030917e35001d2d5e3f2313be19fdb39fb3ca4ba4ec2303b20823" diff --git a/pyproject.toml b/pyproject.toml index ecee01d..99070e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ coverage = "^7.3.1" flake8 = "^6.1.0" django-extensions = "^3.2.3" ipython = "^8.15.0" +django-filter = "^23.3" [build-system]