当我请求' DELETE ... / id / delete'时,我在DRF的回复中找不到404.
我的模型的一部分有一个成功的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 -
它来自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()
有效!
答案 0 :(得分:0)
我不确切知道原因,但问题出在查询集中。
没关系:
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))