3

I have made a filter api like this.

localhost/api/allpackages?price_min=700&price_max=900&destination=Spain&new_activity=Swimming&tour_type=Group%20Tour&featured=true&fix_departure=true

But according to new changes, I should be able to filter like this

localhost/api/allpackages?destination=Spain&destination=Japan&destination=Thailand....featured=true...

There can be multiple values for a single parameter, beacause user can now clik the chekboxes on the frontend. How can I achieve this?

My models:

class Package(models.Model):
    operator = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    destination = models.ForeignKey(Destination, on_delete=models.CASCADE)
    package_name = models.CharField(max_length=255)
    featured = models.BooleanField(default=False)
    price = models.IntegerField()
    duration = models.IntegerField(default=5)
    discount = models.CharField(max_length=255, default="15% OFF")
    discounted_price = models.IntegerField(default=230)
    savings = models.IntegerField(default=230)
    tour_type = models.CharField(max_length=100, choices=TOUR_TYPE, default='Group Tour')
    new_activity = models.ManyToManyField(NewActivity)
    accommodation = models.CharField(max_length=255,default='Guest House & Hotel')
    transport = models.CharField(max_length=150, default='Flight')
    age_range = models.CharField(max_length=100, default='6 to 79 years old')
    fix_departure = models.BooleanField(default=False)
....
...

My views:

class AllPackageAPIView(ListAPIView):
    queryset = Package.objects.all()
    serializer_class = PackageSerializer
    filterset_class = PackageFilter
    
 
    def get_queryset(self):
        new_activity = self.request.query_params.get('new_activity', None)
        destination = self.request.query_params.get('destination', None)
        if new_activity is not None:
            if destination is not None:
                return Package.objects.filter(destination__name=destination, new_activity__title=new_activity)
            else:
                return Package.objects.filter(new_activity__title=new_activity)
        elif destination is not None:
            if new_activity is not None:
                return Package.objects.filter(destination__name=destination, new_activity__title=new_activity)
            else:
                return Package.objects.filter(destination__name=destination)
        else:
            return Package.objects.all()

My filter:

class PackageFilter(filters.FilterSet):
   price = filters.RangeFilter()

   class Meta:
      model = Package
      fields = ['price','featured', 'fix_departure',
                'tour_type',]

My serializers:

class PackageSerializer(serializers.ModelSerializer):
  
    class Meta:
        model = Package
        fields = ['id', 'operator','destination', 'package_name', 'duration', 'featured', 'price', 'discount', 'discounted_price',
                       'tour_type','new_activity', 'accommodation', 'transport', 'age_range',
                  'savings', 'fix_departure', 'rating', 'image', 'date_created', ]
        # fields = '__all__'
        depth = 1

I have done this but now no data are shown. The get list is empty. I used get_queryset(self) as a function and now self.request.GET.get for querying.

MY updated view:

class AllPackageAPIView(ListAPIView):
    queryset = Package.objects.all()
    serializer_class = PackageSerializer
    filterset_class = PackageFilter

def get_queryset(self): 
    new_activity = self.request.GET.get('new_activity', None)
    destination = self.request.GET.get("destination", "")
    destination_values = destination.split(",")
    if new_activity is not None:
        if destination is not None:
            return Package.objects.filter(destination__name=destination_values, new_activity__title=new_activity)
        else:
            return Package.objects.filter(new_activity__title=new_activity)
    elif destination is not None:
        if new_activity is not None:
            return Package.objects.filter(destination__name=destination_values, new_activity__title=new_activity)
        else:
            return Package.objects.filter(destination__name=destination_values)
    else:
        return Package.objects.all()

My solution:

def get_queryset(self):
# def get(self, request, format=None, *args, **kwargs):

    new_activity = self.request.GET.get('new_activity',None)
    destination = self.request.GET.get("destination",None)
    tour_type = self.request.GET.get("tour_type",None)
    if new_activity is not None:
        new_activity = self.request.GET.get('new_activity', "")
        new_activity_values = new_activity.split(",")
        if destination is not None:
            destination = self.request.GET.get("destination", "")
            destination_values = destination.split(",")
            if tour_type is not None:
                tour_type = self.request.GET.get("tour_type", "")
                tour_type_values = tour_type.split(",")
                return Package.objects.filter(destination__name__in=destination_values,new_activity__title__in=new_activity_values,
                                          tour_type__in=tour_type_values)
            else:
                return Package.objects.filter(destination__name__in=destination_values,
                                              new_activity__title__in=new_activity_values)
        else:
            return Package.objects.filter(new_activity__title__in=new_activity_values)
    elif destination is not None:
        destination = self.request.GET.get("destination", "")
        destination_values = destination.split(",")
        if new_activity is not None:
            new_activity = self.request.GET.get('new_activity', "")
            new_activity_values = new_activity.split(",")
            if tour_type is not None:
                tour_type = self.request.GET.get("tour_type", "")
                tour_type_values = tour_type.split(",")
                return Package.objects.filter(destination__name__in=destination_values,
                                              new_activity__title__in=new_activity_values,
                                              tour_type__in=tour_type_values)
            else:
                return Package.objects.filter(destination__name__in=destination_values,
                                              new_activity__title__in=new_activity_values
                                              )
        else:
            return Package.objects.filter(destination__name__in=destination_values)
    elif tour_type is not None:
        tour_type = self.request.GET.get("tour_type", "")
        tour_type_values = tour_type.split(",")
        if destination is not None:
            destination = self.request.GET.get("destination", "")
            destination_values = destination.split(",")
            if new_activity is not None:
                new_activity = self.request.GET.get('new_activity', "")
                new_activity_values = new_activity.split(",")
                return Package.objects.filter(destination__name__in=destination_values,
                                              new_activity__title__in=new_activity_values,
                                              tour_type__in=tour_type_values)
            else:
                return Package.objects.filter(destination__name__in=destination_values,
                                                       tour_type__in=tour_type_values)
        else:
            return Package.objects.filter(tour_type__in=tour_type_values)
    else:
        return Package.objects.all()

This works as a filter for checkbox searches in ecommerce website. But it has a problem. When calling api, it repeats some of the objects ie same package object in my case. If anyone can solve it, let me know.

Reactoo
  • 916
  • 2
  • 12
  • 40

3 Answers3

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:

  • List all users
  • Filter by first_name, last_name, and username
  • Filter by usernameenter image description here
Vu Phan
  • 594
  • 3
  • 8
  • return Package.objects.filter(destination__name=destination) so this should be replaced with return Package.objects.filter(destination__name=destination_values)?? after writing above code?? – Reactoo Jan 18 '21 at 10:38
  • Just correct your code is `return Package.objects.filter(destination__name__in= destination_values)`. This is a way when using `__in`. Or Q object to implement OR condition (https://docs.djangoproject.com/en/2.0/topics/db/queries/#complex-lookups-with-q-objects) – Vu Phan Jan 18 '21 at 12:03
  • I am getting this error 'list' object has no attribute 'split'. How to solve this? @vu Phan – Reactoo Jan 18 '21 at 12:10
  • |The main url ie localhost/api/allpackages. I havent put any parameters, yet it shows this error. How to solve it?? – Reactoo Jan 18 '21 at 14:12
  • Got the error, because this `destination = self.request.GET.get("destination", [])`. Updated my answer: `destination = self.request.GET.get("destination", "")` – Vu Phan Jan 18 '21 at 14:39
  • Hi Vu Phan, the error is removed but now the api is empty ie no data are shown. I have updated my view which you can see above. – Reactoo Jan 18 '21 at 17:37
  • Query filter is missing __in – Vu Phan Jan 19 '21 at 01:24
  • It still doesnt work, i think its related with get_queryset(self), i think it cant be used with GET.get. Can you help @Vu Phan – Reactoo Jan 19 '21 at 03:05
  • Make sure the URL contains the param and value `localhost/api/allpackages?destination=Spain,Japan,Thailand`; make sure `destination` table has `Spain`,... It's not related to get_queryset(self), you can get the value of `destination` in that method or list() method. – Vu Phan Jan 19 '21 at 03:21
  • I think this has a problem, if I have to only select localhost/api/allpackages?featured=true&tour_type=Group Tour, it doesnt return anything, I have to include destination parameter always. How can we recover this?? – Reactoo Jan 19 '21 at 05:22
  • Seem your code does not includes handling code for ‘featured’ and ‘tour_type’ parameter. – Vu Phan Jan 19 '21 at 05:33
  • You can override list method as well, make sure you cover all the case with every parameter. – Vu Phan Jan 19 '21 at 05:36
  • No, I have packagefilter there if you can see, it includes featured and tour_type parameter. The problem i can search through featured= true but for that i have to include destination paramter in the url always, otherwise it doesnt work. – Reactoo Jan 19 '21 at 05:37
  • Got it, you right. It’s related to overriding get_queryset(), try to override list or get method as well. – Vu Phan Jan 19 '21 at 05:39
  • when using get(self,request,*args,**kwargs), you have to return Response(serilaizer.data) which is not possible in my case.How to overwrite get or list and use my code??? do you have any idea? – Reactoo Jan 19 '21 at 05:46
  • I think package django_filter work based on returning get_queryset method. You should read the code base of django_filter first. The idea is query filter by the field “destination” based on returning result in django_filter. I’m sure. – Vu Phan Jan 19 '21 at 05:51
  • Okay, I will remove PackageFilter and put every parameter of the filter inside get method, but if I have localhost/api/allpackages, it wont return anything, i have to include at least one parameter in the url. – Reactoo Jan 19 '21 at 06:00
  • Dont forget check “” or None value before using filter query. – Vu Phan Jan 19 '21 at 06:02
  • How to do that? CaN you write the code for me?? – Reactoo Jan 19 '21 at 06:05
  • I have updated my solution. Can you have a look at it? @Vu Phan – Reactoo Jan 19 '21 at 09:20
  • @Saroj Paudel Sad :( I will give a hand for coding the sample tonight – Vu Phan Jan 19 '21 at 09:33
  • Ok let me know @Vu Phan – Reactoo Jan 19 '21 at 09:57
  • Just done, it works well with django-filter package. See the code in my answer. – Vu Phan Jan 19 '21 at 15:38
  • The thing is as I said before, If I do your code, my api results will be empty if I didn't include query parameter username in your case (destination in my case). You dont need to include query parameters like first name that is coming from filter_class but you must include username in the query parameter. But, it will give the results if I do it my way for eg using None before entering the if else statements. @Vu Phan – Reactoo Jan 20 '21 at 19:37
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).

Adrian
  • 591
  • 4
  • 12
  • can you please tell me what should be the **URL** for `https://www.example.com/foo?a=1&a=2` in `urls.py` – Azhar Uddin Sheikh Apr 18 '22 at 08:21
  • @AzharUddinSheikh: What about `urlpatterns = [path('foo', views.my_function, name='my_func')]`? This should hand over the request to the function `my_function` with the request as parameter. The `views.my_function` of course only applies, if `my_function` is defined in `views.py`. – Adrian Apr 20 '22 at 08:04
  • This is the right answer, `self.request.query_params.getlist()` is available in django rest framework. – monkut Sep 08 '22 at 05:56
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'), ...}
Rohit Kumar
  • 136
  • 1
  • 6