Django 的 classonly 方法有什么用?

mas*_*ece 3 python django

源代码如下:

class classonlymethod(classmethod):
    def __get__(self, instance, owner):
        if instance is not None:
            raise AttributeError("This method is available only on the view class.")
        return super(classonlymethod, self).__get__(instance, owner)
Run Code Online (Sandbox Code Playgroud)

虽然我可以看到 classonlymethod 只能在类上调用,而不能在实例上调用,这与 python 的 classmethod 不同,为什么我们需要这样的“限制”?

www 上关于 classonlymethod 的内容不多,并且一如既往地赞赏任何外行示例。

Pav*_*sov 5

它在基于类的视图内部使用,as_view向尝试在实例上调用它的人提供描述性错误消息。

我不确定是谁首先决定这是强制性的。