美文网首页
Django rest framework (复杂序列化详解)

Django rest framework (复杂序列化详解)

作者: whenitsallover | 来源:发表于2018-03-12 23:57 被阅读0次

models.py

from django.db import models

# Create your models here.


class UserInfo(models.Model):
    name = models.CharField(max_length=32)
    pwd = models.CharField(max_length=32)
    age = models.IntegerField(null=True,blank=True)
    group = models.ForeignKey(to='Group')
    roles = models.ManyToManyField(to='Role')


class Menu(models.Model):
    name = models.CharField(max_length=32)

class Group(models.Model):
    title = models.CharField(max_length=32)
    mu = models.ForeignKey(to="Menu",default=1)


class Role(models.Model):
    name = models.CharField(max_length=32)

解决方案一:自定义一个类,继承(serializers.CharField,并重写to_representation方法

class MyCharField(serializers.CharField):
                        def to_representation(self, value):
                            data_list = []         #循环value
                            for row in value:
                                data_list.append(row.name)
                            return data_list

                    class UsersSerializer(serializers.Serializer):
                        name = serializers.CharField() # obj.name
                        pwd = serializers.CharField()  # obj.pwd
                        group_id = serializers.CharField() # obj.group_id  
                        xxxx = serializers.CharField(source="group.title") # 通过source 来处理跨表的操作。
                        x1 = serializers.CharField(source="group.mu.name") # obj.group.mu.name
                        # x2 = serializers.CharField(source="roles.all") # obj.mu.name
                        x2 = MyCharField(source="roles.all") # obj.mu.name

解决方案二:和方案一类似,用到了ListField,但是直接拿到对象,构造成字典的方式并返回

class MyCharField(serializers.CharField):
                        def to_representation(self, value):
                            return {'id':value.pk, 'name':value.name}

                    class UsersSerializer(serializers.Serializer):
                        name = serializers.CharField() # obj.name
                        pwd = serializers.CharField()  # obj.pwd
                        group_id = serializers.CharField() # obj.group_id
                        xxxx = serializers.CharField(source="group.title") # obj.group.title
                        x1 = serializers.CharField(source="group.mu.name") # obj.mu.name
                        x2 = serializers.ListField(child=MyCharField(),source="roles.all") # 

解决方案三(推荐):

class UsersSerializer(serializers.Serializer):
                        name = serializers.CharField() # obj.name
                        pwd = serializers.CharField()  # obj.pwd
                        group_id = serializers.CharField() # obj.group_id
                        xxxx = serializers.CharField(source="group.title") # obj.group.title
                        x1 = serializers.CharField(source="group.mu.name") # obj.mu.name
                        # x2 = serializers.CharField(source="roles.all") # obj.mu.name
                        # x2 = serializers.ListField(child=MyCharField(),source="roles.all") # obj.mu.name
                        x2 = serializers.SerializerMethodField()

                        def get_x2(self,obj):
                            obj.roles.all()  ## 
                            role_list = obj.roles.filter(id__gt=1) ##构造条件
                            data_list = [] ## 拿到对象列表
                            for row in role_list:
                                data_list.append({'pk':row.pk,'name':row.name})
                            return data_list
序列化之ModelSerializer
 class UserSerializer(serializers.ModelSerializer):

     class Meta:
         model = models.UserInfo
         fields = '__all__'  # 默认生成所有字段
         depth = 1

class UserView(APIView):
    def get(self,request,*args,**kwargs):
        # users = models.UserInfo.objects.all().values('name','pwd','group__id','group__title')
        # ser = UserSerializer(instance=users, many=True)
        # return Response(users)
        users = models.UserInfo.objects.all()
        ser = UserSerializer(instance=users,many=True)
        return Response(ser.data)

生成局部URL
class UserSerializer(serializers.ModelSerializer):

    group = serializers.HyperlinkedIdentityField(view_name='detail') # view_name 为反向生成URL的名称使用,group字段必须要在fields里。
    class Meta:
        model = models.UserInfo
        # fields = '__all__'
        fields = ['id','name','pwd','group']

urls.py

# url(r'^xxxx/(?P<pk>\d+)', views.UserView.as_view(),name='detail'),  # 带上pk,必须是Pk
生成全局URL(HyperlinkedModelSerialize)

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.UserInfo
        # fields = "__all__"   # URL中要命名为 字段-detail,否则报错

        fields = ['id','name','pwd']

class UserView(APIView):
    def get(self,request,*args,**kwargs):
        # users = models.UserInfo.objects.all().values('name','pwd','group__id','group__title')
        # ser = UserSerializer(instance=users, many=True)
        # return Response(users)
        users = models.UserInfo.objects.all()
        ser = UserSerializer(instance=users,many=True,context={'request':request})
        return Response(ser.data)

相关文章

网友评论

      本文标题:Django rest framework (复杂序列化详解)

      本文链接:https://www.haomeiwen.com/subject/dhzofftx.html