2016-11-18 98 views
0

当我请求'DELETE .../id/delete'时,我得到一个没有被DRF发现的404响应。DRF返回404在DELETE上找不到

我有一个成功的API与我的模型的一部分,但此错误发生在另一部分。

我看不出差异,我不知道如何调试它。

url.py:

url(
     regex=r'^thirdparties/(?P<pk>[0-9]+)/delete/', 
     view=ThirdPartyDestroyAPIView.as_view(), 
     name='thirdparties_delete' 
    ), 

view.py:

@python_2_unicode_compatible 
class ThirdPartyDestroyAPIView(LoginRequiredMixin, PermissionRequiredMixin, CurrentUserThirdPartiesMixin, DestroyAPIView): 
    serializer_class = ThirdPartyReadSerializer 
    permission_required = 'cashflows.delete_thirdparty' 
    raise_exception = True 

我的要求:

DELETE /api/v1/thirdparties/5/delete/ 

我的回应:

django_1 | 172.18.0.1 - - [18/Nov/2016 16:02:11] "DELETE /api/v1/thirdparties/5/delete/ HTTP/1.1" 404 - 

编辑1

它来自get_queryset()方法。 我的方法是在一个mixin:

def get_queryset(self):   
     user = self.request.user 
     if user: 
      if 'category_id' in self.kwargs: 
       return ThirdParty.objects.filter(categories__in=self.kwargs['category_id']) 
      elif 'sheet_id' in self.kwargs: 
       third_parties = ThirdParty.objects.filter(categories__sheet__in=self.kwargs['sheet_id']) 
       third_parties = list(set(third_parties)) 
       return third_parties 
      else: 
       third_parties = ThirdParty.objects.filter(categories__sheet__in=Sheet.objects.filter(user=user)) 
       third_parties = list(set(third_parties)) 
       return third_parties 
     else: 
      return None 

当我将其更改为:

def get_queryset(self): 
     return ThirdParty.objects.all() 

它的工作原理!

+2

你不需要在视图中的'@ python_2_unicode_compatible'装饰。另外,你的整个看法?你应该有一个'get_object'函数来获取特定的'ThirdParty'来删除。目前,该观点不知道要删除什么。 – jape

+0

@jape我认为url中的'pk'参数是要传递给通用视图'DestroyAPIView',不是吗? – bixente57

回答

0

我不知道为什么,但问题出在queryset中。

这是OK:

return ThirdParty.objects.filter(categories__sheet__in=self.kwargs['sheet_id']).distinct() 

这是不正常:

third_parties = ThirdParty.objects.filter(categories__sheet__in=self.kwargs['sheet_id']) 
return third_parties = list(set(third_parties))