DjangoRestFramework使用自定义权限
生活随笔
收集整理的這篇文章主要介紹了
DjangoRestFramework使用自定义权限
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 項目配置
- 項目結構
- 配置項目
- 用戶模型
- 設備模型
- 2. 權限設置
- 自定義權限
- 視圖模型
- 路由設置
- 3. 測試
1. 項目配置
項目結構
構建 Django 項目,結構如下
- MyProj03/
??|__ Assets/
????|__ perms.py
????|__ models.py
????|__ views.py
????|__ urls.py
????|__ ...
??|__ MyProj03/
????|__ settings.py
????|__ urls.py
????|__ ...
??|__ UserManagement/
????|__ models.py
????|__ ...
配置項目
MyProj03/MyProj03/settings.py 代碼如下
INSTALLED_APPS = [...'rest_framework','UserManagement','Assets', ]REST_FRAMEWORK = {'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.IsAuthenticated','Devices.perms.RbacPermission', # 自定義權限認證],'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication','rest_framework.authentication.BasicAuthentication','rest_framework.authentication.SessionAuthentication',)}AUTH_USER_MODEL = 'UserManagement.SysUsers'用戶模型
MyProj03/UserManagement/models.py 代碼如下
class SysUsers(AbstractBaseUser):id = models.BigAutoField(primary_key=True)username = models.CharField(max_length=40, unique=True)is_staff = models.BooleanField(('staff status'),default=False,)is_admin = models.BooleanField(default=False)last_login = models.DateTimeField(blank=True, null=True)is_active = models.SmallIntegerField(blank=True, null=True)is_alive = models.SmallIntegerField(blank=True, null=True)objects = UserManager()USERNAME_FIELD = 'username'class Meta:db_table = 'sys_users'verbose_name = "用戶"verbose_name_plural = "用戶"def __str__(self):return self.username添加幾條數據
| 1 | 吳敬中 | 123456 | 0 | 0 | 2021-06-24 14:58:22.118504 | 1 | 1 |
| 2 | 余則成 | 123456 | 0 | 1 | 2021-06-24 14:58:22.118504 | 1 | 1 |
| 3 | 王翠萍 | 123456 | 0 | 0 | 2021-06-24 14:58:22.118504 | 1 | 1 |
| 4 | 穆晚秋 | 123456 | 0 | 0 | 2021-06-24 14:58:22.118504 | 1 | 1 |
設備模型
MyProj03/Assets/models.py 代碼如下
from django.db import models# Create your models here. class Devices(models.Model):id = models.BigAutoField(primary_key=True)name = models.CharField(max_length=32, blank=True, null=True)dept_id = models.BigIntegerField(blank=True, null=True)is_alive = models.SmallIntegerField(blank=True, null=True)class Meta:db_table = 'devices'verbose_name = "設備"verbose_name_plural = "設備"def __str__(self):return self.name添加數據
| 1 | 總經辦電話 | 1 | 1 |
| 2 | 總經辦臺式機1號 | 1 | 1 |
| 3 | 總經辦臺式機2號 | 1 | 1 |
| 4 | 總經辦打印機1號 | 1 | 1 |
| 5 | 市場一部電話1號 | 2 | 1 |
| 6 | 市場一部電話2號 | 2 | 1 |
2. 權限設置
自定義權限
MyProj03/Assets/perms.py
from rest_framework.permissions import BasePermissionclass UserPermission(BasePermission):def has_permission(self, request, view):if not request.user.is_admin:return Truereturn False視圖模型
MyProj03/Assets/views.py
from .models import Assets from rest_framework import serializers, viewsets from Devices.perms import UserPermissionclass DevicesSerializer(serializers.ModelSerializer):class Meta:model = Assetsfields = "__all__"class DevicesViewSet(viewsets.ModelViewSet):permission_classes = [UserPermission, ]queryset = Assets.objects.all()serializer_class = DevicesSerializer路由設置
MyProj03/Assets/urls.py
from django.urls import path, include from rest_framework import routersfrom Assets.views import DevicesViewSetrouter = routers.SimpleRouter() router.register('devices', DevicesViewSet)urlpatterns = [path('', include(router.urls)), ]MyProj03/MyProj03/urls.py
from django.urls import path, includeurlpatterns = [path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),path('assets/', include('Assets.urls')), ]3. 測試
使用 余則成 賬號訪問(is_admin==True)數據,可訪問
使用 吳敬中 賬號訪問(is_admin==False)數據,不可訪問
.
.
.
.
.
.
桃花仙人種桃樹,又摘桃花換酒錢_
總結
以上是生活随笔為你收集整理的DjangoRestFramework使用自定义权限的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZUNE连接WP7错误
- 下一篇: 数据恢复技术教程分享