python - self.action is None in DRF
问题描述
self.action
is None
in get_permissions()
method.
When a url is called that doesn't exist DRF doesn't throw 404 error. that's why somehow action is None
in get_permissions()
method.
Here is the ModelViewSet
:
class UserViewSet(ModelViewSet):
serializer_classes = {
"list": UserSerializer,
"retrieve": UserSerializer,
"create": UserSerializer,
"update": UserUpdateSerializer,
}
http_method_names = ["get", "post", "put"]
def get_serializer_class(self):
return self.serializer_classes[self.action]
def get_permissions(self):
if self.action is None:
# here error raises when i call '.../accounts/blablabla/` endpoint in post request instead of 404?
raise AssertionError("self.action cannot be None")
if self.action in ["list", "retrieve"]:
return [IsAuthenticated(), IsAdmin()]
else:
return [AllowAny()]
Here is my discussion link #8199 in github.
解决方案
推荐阅读
- pandas - 如何从具有原始时间戳的 PostgreSQL 数据库中导入数据?
- javascript - 如何在使用 JAVA 进行横向和纵向测试时旋转移动设备。我尝试使用 DesiredCapabilities 但它没有用
- xcode - 为什么我的 xcode 目标文件在我更新后看起来像这样?
- reactjs - 需要或不需要更新状态表上一个状态?反应
- javascript - Angular 应用程序未显示,黑屏不是已知的属性错误
- flutter - 我如何访问列表。Flutter中方法构造函数的参数
- ios - IOS颤振构建没有创建,但它在android上运行
- javascript - 添加 vue 组件会导致 'parentNode' TypeError
- javascript - 如果我缩小窗口而不是移动设备,为什么我的断点可以工作?
- regex - Pyspark 处理用逗号转十进制的数字