当前位置: 首页 > 工具软件 > rest-layer > 使用案例 >

DRF(Django-rest-framework)增删改查

左丘昊天
2023-12-01

该篇帖子使用环境:

python==3.7 | django==3.0.2 | django-rest-framework==3.11.0

数据库模型 model.py

class SysPermission(models.Model):
    id = models.CharField(db_column='Id', primary_key=True, max_length=50)  # Field name made lowercase.
    parentid = models.CharField(db_column='ParentId', max_length=50, blank=True, null=True)  # Field name made lowercase.
    layer = models.IntegerField(db_column='Layer', blank=True, null=True)  # Field name made lowercase.
    encode = models.CharField(db_column='EnCode', max_length=50, blank=True, null=True)  # Field name made lowercase.
    name = models.CharField(db_column='Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    jsevent = models.CharField(db_column='JsEvent', max_length=50, blank=True, null=True)  # Field name made lowercase.
    icon = models.CharField(db_column='Icon', max_length=50, blank=True, null=True)  # Field name made lowercase.
    url = models.CharField(db_column='Url', max_length=255, blank=True, null=True)  # Field name made lowercase.
    remark = models.CharField(db_column='Remark', max_length=255, blank=True, null=True)  # Field name made lowercase.
    type = models.IntegerField(db_column='Type', blank=True, null=True)  # Field name made lowercase.
    sortcode = models.IntegerField(db_column='SortCode', blank=True, null=True)  # Field name made lowercase.
    ispublic = models.IntegerField(db_column='IsPublic', blank=True, null=True)  # Field name made lowercase.
    isenable = models.IntegerField(db_column='IsEnable', blank=True, null=True)  # Field name made lowercase.
    isedit = models.IntegerField(db_column='IsEdit', blank=True, null=True)  # Field name made lowercase.
    deletemark = models.IntegerField(db_column='DeleteMark', blank=True, null=True)  # Field name made lowercase.
    createuser = models.CharField(db_column='CreateUser', max_length=50, blank=True, null=True)  # Field name made lowercase.
    createtime = models.DateTimeField(db_column='CreateTime', max_length=50, blank=True, null=True, auto_now_add=True)  # Field name made lowercase.
    modifyuser = models.CharField(db_column='ModifyUser', max_length=50, blank=True, null=True)  # Field name made lowercase.
    modifytime = models.DateTimeField(db_column='ModifyTime', max_length=50, blank=True, null=True, auto_now=True)  # Field name made lowercase.

    class Meta:
        db_table = 'sys_permission'

序列器 serializers.py

from rest_framework import serializers
import uuid
from .models import SysPermission
class sysPermissionSerializer(serializers.ModelSerializer):
    modifytime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S",  read_only=True)
    children = serializers.SerializerMethodField()
    def get_children(self, data):
        children = SysPermission.objects.filter(parentid=data.id).all().values()
        return children

    class Meta:
        model = SysPermission
        fields = ('id', 'parentid', 'name', 'icon',  'type', 'url', 'sortcode', 'isenable', 'jsevent', 'modifytime', 'children')
        read_only_fields = ('id',)

    def validate(self, user):  # 对数据进行校验,中间对数据为所欲为
        user['id'] = uuid.uuid4()
        return user

视图 viwes.py

from rest_framework import generics, mixins #视图类
from rest_framework import viewsets #视图类
from .serializers import sysPermissionSerializer #序列器
from .models import SysPermission #数据库模型
class sysPermissionViewSet(viewsets.ModelViewSet):
    """
    获取菜单接口
    """
    queryset = SysPermission.objects.filter(parentid__in='0').all()
    serializer_class = sysPermissionSerializer
    # pagination_class = sysUserSetPagination
    def getmore(self, request, *args, **kwargs):
        """查看单条数据"""
        pk = kwargs.get('pk')
        queryset = SysPermission.objects.filter(id=pk).all()
        serializer = self.get_serializer(instance=queryset, many=True)
        return Response(serializer.data)

路由 urls.py

	path('sysPermission', sysPermissionViewSet.as_view({'get':'list'})),
    re_path('sysPermission/(?P<pk>.*)', sysPermissionViewSet.as_view({'get': 'getmore'}))
博主,正在学习DRF中;如有问题,请联系博主修改(或有需要优化的地方),谢谢大家!
 类似资料: