问题描述
我在这段代码上有一个pylint消息(w0707)(来自https://www.django-rest-framework.org/tutorial/3-class-based-views/):
class SnippetDetail(APIView):
"""
Retrieve,update or delete a snippet instance.
"""
def get_object(self,pk):
try:
return Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
raise Http404
消息是:Consider explicitly re-raising using the 'from' keyword
我不太了解如何解决问题。
预先感谢您的帮助
解决方法
上面关于您的问题的评论中的链接概述了该问题并提供了解决方案,但是为了使那些像我一样直接登陆该页面的人清晰起见,而无需转到另一个线程,阅读并获取上下文,这是您特定问题的答案:
TL; DR;
这可以通过将您正在“例外”的异常别名化并在第二次加薪中引用它来解决。
在上面的代码段中,请参见最后两行,我添加了“插入符号”以表示我添加的内容。
class SnippetDetail(APIView):
"""
Retrieve,update or delete a snippet instance.
"""
def get_object(self,pk):
try:
return Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist as snip_no_exist:
# ^^^^^^^^^^^^^^^^
raise Http404 from snip_no_exist
# ^^^^^^^^^^^^^^^^^^
注意:别名可以是任何格式正确的字符串。