MOH*_*EEN 5 django python-3.x django-rest-framework
我正在为产品创建 REST API,它具有以下权限(create_product、view_product、edit_product)。在我的项目中,我有不同角色的各种用户(例如:生产者、零售商、消费者等)。我正在向各个角色分配权限。我正在使用 Django 组权限
示例:“生产者”角色具有“create_product”和“view_product”权限。“零售商”角色具有“edit_product”权限。“消费者”角色没有权限。
我想根据权限代码限制访问。我需要一个通用的方法来解决这个问题。我想对具有不同权限代码的不同视图使用相同的方法。
在我看来.py,
class Product(viewsets.ModelViewSet):
serializer_class = ProductSerializer
queryset = Product.objects.all()
Run Code Online (Sandbox Code Playgroud)
在 settings.py 中,我添加了以下代码。
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
| 归档时间: |
|
| 查看次数: |
2297 次 |
| 最近记录: |