• DRF(Django-Rest-FrameWork)非主外键自关联


    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'

    序列器

    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):
            queryset = SysPermission.objects.filter(parentid=data.id).all()
            children = sysPermissionSerializer(queryset,many=True).data
            if children:
                return children
            else:
                return ""
    
        class Meta:
            model = SysPermission
            fields = ('id', 'parentid', 'name', 'icon',  'type', 'url', 'sortcode', 'isenable', 'jsevent', 'modifytime', 'children')
            read_only_fields = ('id',)

    views.py

    class sysPermissionViewSet(viewsets.ModelViewSet):
        """
        菜单接口
        """
        queryset = SysPermission.objects.all()
        serializer_class = sysPermissionSerializer
        # filter_fields = ('id',)
        # pagination_class = sysUserSetPagination
    
        def get(self, request:Response, *args, **kwargs):
            code = Response.status_code
            queryset = SysPermission.objects.filter(parentid__in='0').all()
            serializer = self.get_serializer(instance=queryset, many=True)
            json = returnJson(code, serializer.data) #returnJson是我自己封装的返回方法
            return Response(json)

    urls.py

    path('sysPermission', sysPermissionViewSet.as_view({'get':'get'})),

    最后效果:

    在这里插入图片描述

  • 相关阅读:
    java keytool证书工具使用小结(转)
    Https socket 代理
    SSL连接出现的问题
    Https 代理 sslsocket
    Https socket 连接
    linux系统一键安装phpstudy的lnmp环境
    Yii2框架实现计数器功能
    yii2框架增删改查案例
    yii2框架原生的结合框架使用的图片上传
    php将抓取的图片链接下载到本地
  • 原文地址:https://www.cnblogs.com/catgatp/p/13795258.html
Copyright © 2020-2023  润新知