@property
def user_settings(self):
if not hasattr(self, '_user_settings'):
# _user_settings預設為載入專案settings檔案中的REST_FRAMEWORK物件
self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
return self._user_settings
def __getattr__(self, attr):
if attr not in self.defaults:
raise AttributeError("Invalid API setting: '%s'" % attr)
try:
# Check if present in user settings# 優先載入user_settings,即專案的settings檔案,沒有就用預設
val = self.user_settings[attr]
except KeyError:
# Fall back to defaults
val = self.defaults[attr]
# Coerce import strings into classesif attr in self.import_strings:
val = perform_import(val, attr)
# Cache the result
self._cached_attrs.add(attr)
setattr(self, attr, val)
return val
複製程式碼
defget_authenticators(self):"""
Instantiates and returns the list of authenticators that this view can use.
"""return [auth() for auth in self.authentication_classes]
複製程式碼
通過指定的permission_classes確定是否有當前介面的訪問許可權:
classIsAuthenticatedOrReadOnly(BasePermission):"""
The request is authenticated as a user, or is a read-only request.
"""defhas_permission(self, request, view):return (
request.method in SAFE_METHODS or
request.user and
request.user.is_authenticated
)
複製程式碼