Django Rest Framework URL参数

huangapple go评论50阅读模式
英文:

Django Rest Framework URL parameters

问题

目前我需要显示特定工作坊的所有客户,然后我正在使用以下URL:http://localhost:8000/customer-list/?workshop_id=1

在我的视图中,我有以下实现:

class CustomerList(ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
    filter_backends = [SearchFilter]
    search_fields = ['customer_name']
    
    def filter_queryset(self, queryset):
        workshop_id = self.request.query_params.get('workshop_id', None)
        if workshop_id is not None:
            queryset = queryset.filter(workshop_id=workshop_id)
        return queryset

    def list(self, request, *args, **kwargs):
        workshop_id = self.request.query_params.get('workshop_id', None)
        
        if not workshop_id:
            return Response({"status": "Required field not found."}, status=status.HTTP_404_NOT_FOUND)
        return super(CustomerList, self).list(request, *args, **kwargs)

URL路径看起来像这样:

path('customer-list/', views.CustomerList.as_view(), name='customer_list'),

但我想要我的URL看起来像这样:http://localhost:8000/{workshop_id}/customer-list

如何设置我的URL路径:

以及如何在客户视图中获取workshop_id以应用过滤器:

我尝试更改URL模式,但没有成功。

英文:

Currently i have to display all customers for a specific workshop then I am using following url:
http://localhost:8000/customer-list/?workshop_id=1

and in my view I have following implementation:

class CustomerList(ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
    filter_backends = [SearchFilter]
    search_fields = ['customer_name']
    
    def filter_queryset(self, queryset):
        workshop_id = self.request.query_params.get('workshop_id', None)
        if workshop_id is not None:
            queryset = queryset.filter(workshop_id=workshop_id)
        return queryset
    def list(self,request,*args,**kwargs):
        workshop_id = self.request.query_params.get('workshop_id', None)
        
        if not (workshop_id):
            return Response({"status": "Required field not found."},
                                        status=status.HTTP_404_NOT_FOUND)
        return super(CustomerList, self).list(request,*args,**kwargs)

Url Path looks like this:

path('customer-list/', views.CustomerList.as_view(),name='customer_list'),

But I want my url should look like this:
http://localhost:8000/{workshop_id}/customer-list

How can I set my URL path:

and how can I get workshop_id in customer view to apply filters:

I have tried to change url pattern but it did not work.

答案1

得分: 0

只需在路径中定义workshop_id作为URL参数,例如:

path('<int:workshop_id>/customer-list/', views.CustomerList.as_view(), name='customer_list')

然后,您可以从self.kwargs中获取参数:

class CustomerList(ListAPIView):
    ...
    def list(self, request, *args, **kwargs):
        workshop_id = self.kwargs.get('workshop_id', None)
        ...
英文:

To pass workshop_id as an URL parameter just define it in the path, for example:

path(&#39;&lt;int:workshop_id&gt;/customer-list/&#39;, views.CustomerList.as_view(),name=&#39;customer_list&#39;)

And you can get the parameter from self.kwarg:

class CustomerList(ListAPIView):
    ...
    def list(self, request, *args, **kwargs):
        workshop_id = self.kwargs.get(&#39;workshop_id&#39;, None)
        ...

huangapple
  • 本文由 发表于 2023年2月19日 06:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定