如何在Django Rest Framework中将单个参数的多个值传递到API url中?

9w11ddsr  于 2023-10-21  发布在  Go
关注(0)|答案(3)|浏览(138)

我做了一个像这样的过滤器API:

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

但根据新的变化,我应该可以像这样过滤:

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

一个参数可以有多个值,因为用户现在可以点击前端的复选框。我如何才能做到这一点?
我的模特:

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)
....
...

我的观点:

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()

我的过滤器:

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

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

我的序列化器:

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

我已经这样做了,但现在没有数据显示。获取列表为空。我使用get_queryset(self)作为函数,现在使用self.request.GET.get进行查询。
我的最新观点:

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()

我的解决方案:

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()

这可以作为电子商务网站中复选框搜索的过滤器,但它有一个问题。当调用API时,它会重复一些对象,即在我的案例中,相同的包对象。如果有人能解决它,让我知道。

fsi0uk1n

fsi0uk1n1#

我以前解决过这个问题,我决定通过使用分割,字符来获得URL中的多个值。
示例:URL:localhost/api/allpackages?destination=Spain,Japan,Thailand....featured=true...

destination = self.request.GET.get("destination", "")

destination_values = destination.split(",")

关于在User模型中过滤first_namelast_nameusername的多个值的示例代码。
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()

结果如下:

  • 按用户名筛选e
bksxznpy

bksxznpy2#

我发现Django至少从3.0开始就支持QueryDict的多值参数。所以当你遇到这样的情况时:

https://www.example.com/foo?a=1&a=2

你可以通过以下方式得到a的所有值:

def my_function(request):
    a_list = request.query_params.getlist('a')
    # [1, 2]

这并不直观,因为request.query_params.get('a')只返回列表中的最后一个元素(参见文档)。

1mrurvl1

1mrurvl13#

django-rest-framework不提供多值过滤器支持,如果你想或者你可以使用djangorestframework-jsonapi它提供了多值过滤器和许多其他可插入的功能
值列表中的成员:?filter[name.in]=abc,123,zzz(name in 'abc','123 ','zzz'])
您可以通过设置REST_FRAMEWORK“DEFAULT_FILTER_RESERVENDS”]来配置筛选器后端,也可以将它们单独添加为.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',
    ),

请参见此示例https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#configuring-filter-backends
您的代码有更改,您不需要编写PackageFilter和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'), ...}

相关问题