回答问题

我在这段代码上有一条 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

我不太明白如何采取行动来纠正问题。

Answers

上面对您的问题的评论中的链接概述了问题并提供了解决方案,但是为了清楚那些像我一样直接登陆此页面的人,而不必转到另一个线程,阅读并获得上下文,这是您的答案具体问题:

zoz100007 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
#                         ^^^^^^^^^^^^^^^^^^

注意:别名可以是任何格式正确的字符串。

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐