更改 URL 对 [=10th=] 返回的 Django Rest Framework 没有影响

Changing the URL has no effect on JSON Returned DjangoRest Framerwork

我正在尝试过滤我存储的客户列表和 return 特定的客户 当我尝试 (xxx/api/customers/fred) 时,它 return 是所有客户以及在客户之后输入的任何内容/对 JSON returned

没有影响

观看次数

class CustomerListAPIView(generics.ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

class CustomerRetrieveAPIView(generics.RetrieveAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
    lookup_field= "name"

序列化程序

class CustomerSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Customer 

        fields = ['name' , 'address', 'phonenumber']

网址

url(r'^api/customers/', views.CustomerListAPIView.as_view(), name = "customer_list"),
url(r'^(?P<slug>[\w-]+)/$', views.CustomerRetrieveAPIView.as_view(), name='retrieve'),

我还尝试覆盖 def get_queryset(self, *args, **kwargs): 但是当输入 url 时似乎不会触发此方法

将更详细的网址放在更一般的网址之前以避免歧义,将 $ 添加到 customer_list URL 模式的末尾,将缺少的前缀添加到 retrieve URL 模式:

url(r'^api/customers/(?P<slug>[\w-]+)/$', views.CustomerRetrieveAPIView.as_view(), name='retrieve'),
url(r'^api/customers/$', views.CustomerListAPIView.as_view(), name = "customer_list"),

您的所有请求均由第一种模式处理 - 因为(列表和详细信息)URL 都以 api/customers/ 开头。