[Django]-What is the difference between django classonlymethod and python classmethod?

56👍

✅

The best explanation is the source code itself:

class classonlymethod(classmethod):
    def __get__(self, instance, cls=None):
        if instance is not None:
            raise AttributeError("This method is available only on the class, not on instances.")
        return super().__get__(instance, cls)

The difference is that a classmethod can be called on an instance, having the same effect as calling it on the class, but the classonlymethod can only be called on the class.

Leave a comment