3👍
I have solved this question before, I’ve decided to get multiple values in URL by using split ,
character.
Example: URL: localhost/api/allpackages?destination=Spain,Japan,Thailand....featured=true...
destination = self.request.GET.get("destination", "")
destination_values = destination.split(",")
Sample code about filtering first_name
, last_name
, and multiple values of username
in User model.
model.py
class User(AbstractUser):
@property
def full_name(self):
"""Custom full name method as a property"""
return str(self.first_name) + ' ' + str(self.last_name)
def __str__(self):
return self.email
view.py
class UserFilter(filters.FilterSet):
class Meta:
model = User
fields = ['first_name', 'last_name']
class ListCreateUser(ListCreateAPIView):
"""
List and Create User Generic contains create and list user APIs.
"""
serializer_class = UserSerializer
queryset = User.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = UserFilter
def get_queryset(self):
username = self.request.GET.get('username', '')
if username:
username_values = username.split(',')
return User.objects.filter(username__in=username_values)
return User.objects.all()
Results:
2👍
I found that Django supports multi-value parameters with its QueryDict since at least 3.0 . So when you have the situation like:
https://www.example.com/foo?a=1&a=2
you can get all values of a
with:
def my_function(request):
a_list = request.query_params.getlist('a')
# [1, 2]
This is not intuitive, since request.query_params.get('a')
only returns the last element in the list (see documentation).
- [Django]-Save ManyToMany with through intermediary class
- [Django]-What file or module actually generates "It worked" in Django tutorials?
- [Django]-Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename
1👍
django-rest-framework does not provide multi-value filter support, you have to write it yourself if you want OR you can use djangorestframework-jsonapi it provides the multi-value filter and many other pluggable features
Membership in a list of values: ?filter[name.in]=abc,123,zzz (name in [‘abc’,’123′,’zzz’])
You can configure the filter backends either by setting the REST_FRAMEWORK[‘DEFAULT_FILTER_BACKENDS’] or individually add them as .filter_backends
'DEFAULT_FILTER_BACKENDS': (
'rest_framework_json_api.filters.QueryParameterValidationFilter',
'rest_framework_json_api.filters.OrderingFilter',
'rest_framework_json_api.django_filters.DjangoFilterBackend',
'rest_framework.filters.SearchFilter',
),
See this example
https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#configuring-filter-backends
Your code with changes you don’t need to write PackageFilter and get_queryset
from rest_framework_json_api import django_filters
class AllPackageAPIView(ListAPIView):
queryset = Package.objects.all()
serializer_class = PackageSerializer
filter_backends = (django_filters.DjangoFilterBackend)
filterset_fields = {'destination': ('exact', 'in'), ...}
- [Django]-Django project /admin Site matching query does not exist
- [Django]-Django query for all items from today