Saq*_*Ali 4 python django rest
我正在构建一个Django应用程序,它公开了一个REST API,用户可以通过它查询我的应用程序的模型.我按照这里的说明操作.
下面你可以看到我从命令行用各种用户名/密码命中这个API.但是,它只有在我使用root用户和密码时才有效.为什么?我该如何改变?我没有在任何地方指定此API仅供root用户使用.我希望它能公开发布
% curl -H 'Accept: application/json; indent=4' -u root:myRootPassword http://127.0.0.1:3001/api/profiles/60/
{
"id": 60,
"slug": "my_user",
"user": "http://127.0.0.1:3001/api/users/16/"
}
% curl -H 'Accept: application/json; indent=4' http://127.0.0.1:3001/api/users/16/
{
"detail": "Authentication credentials were not provided."
}
% curl -H 'Accept: application/json; indent=4' -u myUser:myPassword http://127.0.0.1:3001/api/profiles/60/
{
"detail": "You do not have permission to perform this action."
}
% curl -H 'Accept: application/json; indent=4' -u myUser:myPassword http://127.0.0.1:3001/api/profiles/60/
{
"detail": "Invalid username/password"
}
Run Code Online (Sandbox Code Playgroud)
在你的APIView或你的ModelViewSet中
permission_classes = []
Run Code Online (Sandbox Code Playgroud)
要么
permission_classes = [rest_framework.permissions.AllowAny]
Run Code Online (Sandbox Code Playgroud)
这将使其公开适用于任何人.这是因为所有modeviewsets/viewsets /或APIView都来自APIView,它们将权限类设置为
permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES
Run Code Online (Sandbox Code Playgroud)
我猜你的情况只是一个超级用户.
好的只是看看你关注的指南.如果你看看你的设置
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'PAGINATE_BY': 10
}
Run Code Online (Sandbox Code Playgroud)
您将默认权限类设置为仅限管理员.因此,您可以执行我之前建议的操作并覆盖默认权限,也可以将IsAdminUser更改为
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',),
'PAGINATE_BY': 10
}
Run Code Online (Sandbox Code Playgroud)
祝你好运,django-rest-framework令人惊叹.
| 归档时间: |
|
| 查看次数: |
5466 次 |
| 最近记录: |