序列化
1.继承Serializer
基本使用
(1)models.py
from django.db import models class UserInfo(models.Model): USER_TYPE = ( (1,'普通用户'), (2,'VIP'), (3,'SVIP') ) user_type = models.IntegerField(choices=USER_TYPE) username = models.CharField(max_length=32,unique=True) password = models.CharField(max_length=64) group = models.ForeignKey('UserGroup',on_delete=models.CASCADE) roles = models.ManyToManyField('Role') class UserToken(models.Model): user = models.OneToOneField('UserInfo',on_delete=models.CASCADE) token = models.CharField(max_length=64) class UserGroup(models.Model): title = models.CharField(max_length=32) class Role(models.Model): title = models.CharField(max_length=32)
录入一些数据
2)api/urls.py
from django.conf.urls import url, include
from api.views import RolesView
urlpatterns = [
#序列化
url('(?P<version>[v1|v2]+)/roles/', RolesView.as_view()),
]
(3)views.py
import json from django.shortcuts import render,HttpResponse from rest_framework.views import APIView from . import models from rest_framework import serializers #要先写一个序列化的类 class RolesSerializer(serializers.Serializer): #Role表里面的字段id和title序列化 id = serializers.IntegerField() title = serializers.CharField() class RolesView(APIView): def get(self,request,*args,**kwargs): # 方式一:对于[obj,obj,obj] # (Queryset) roles = models.Role.objects.all()
# 序列化,两个参数,instance:接受Queryset(或者对象) mangy=True表示对Queryset进行处理,many=False表示对对象进行进行处理 ser = RolesSerializer(instance=roles,many=True) # 转成json格式,ensure_ascii=False表示显示中文,默认为True
ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)
(4)浏览器访问:http://127.0.0.1:8000/api/v1/roles/
可以显示后台返回的json数据
(5)方式二
class RolesView(APIView): def get(self,request,*args,**kwargs): # 方式一:对于[obj,obj,obj] # (Queryset) # roles = models.Role.objects.all() # 序列化,两个参数,instance:Queryset 如果有多个值,就需要加 mangy=True # ser = RolesSerializer(instance=roles,many=True) # 转成json格式,ensure_ascii=False表示显示中文,默认为True # ret = json.dumps(ser.data,ensure_ascii=False) # 方式二: role = models.Role.objects.all().first() ser = RolesSerializer(instance=role, many=False) ret = json.dumps(ser.data, ensure_ascii=False) return HttpResponse(ret)
只获取一个
进阶使用
(多对多 用户与角色 多对一 用户与分组)
(1)urls.py
添加一个info
from django.conf.urls import url, include
from api.views import RolesView,UserInfoView
urlpatterns = [
url('(?P<version>[v1|v2]+)/roles/', RolesView.as_view()),
url('(?P<version>[v1|v2]+)/info/', UserInfoView.as_view()),
]
(2)views.py
class UserInfoSerializer(serializers.Serializer):'''序列化用户的信息''' #user_type是choices(1,2,3),显示全称的方法用source type = serializers.CharField(source="get_user_type_display") username = serializers.CharField() password = serializers.CharField() #group.title:组的名字 group = serializers.CharField(source="group.title")
#SerializerMethodField(),表示自定义显示
#然后写一个自定义的方法 rls = serializers.SerializerMethodField() def get_rls(self,row): #获取用户所有的角色 role_obj_list = row.roles.all() ret = [] #获取角色的id和名字 #以字典的键值对方式显示 for item in role_obj_list: ret.append({"id":item.id,"title":item.title}) return ret class UserInfoView(APIView): '''用户的信息''' def get(self,request,*args,**kwargs): users = models.UserInfo.objects.all() ser = UserInfoSerializer(instance=users,many=True) ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)
(3)浏览器访问:http://127.0.0.1:8000/api/v1/info/
2.继承 ModelSerializer
把上面的UserInfoSerializer改成继承ModelSerializer的用法
# class UserInfoSerializer(serializers.Serializer): # '''序列化用户的信息''' # #user_type是choices(1,2,3),显示全称的方法用source # type = serializers.CharField(source="get_user_type_display") # username = serializers.CharField() # password = serializers.CharField() # #group.title:组的名字 # group = serializers.CharField(source="group.title") # #SerializerMethodField(),表示自定义显示 # #然后写一个自定义的方法 # rls = serializers.SerializerMethodField() # # def get_rls(self,row): # #获取用户所有的角色 # role_obj_list = row.roles.all() # ret = [] # #获取角色的id和名字 # #以字典的键值对方式显示 # for item in role_obj_list: # ret.append({"id":item.id,"title":item.title}) # return ret class UserInfoSerializer(serializers.ModelSerializer): type = serializers.CharField(source="get_user_type_display") group = serializers.CharField(source="group.title") rls = serializers.SerializerMethodField() def get_rls(self, row): # 获取用户所有的角色 role_obj_list = row.roles.all() ret = [] # 获取角色的id和名字 # 以字典的键值对方式显示 for item in role_obj_list: ret.append({"id": item.id, "title": item.title}) return ret class Meta: model = models.UserInfo fields = ['id','username','password','type','group','rls'] class UserInfoView(APIView): '''用户的信息''' def get(self,request,*args,**kwargs): users = models.UserInfo.objects.all() ser = UserInfoSerializer(instance=users,many=True) ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)
结果一模一样
3.自动序列化连表(depth)
继续优化上面的代码,用depth更简单方便
class UserInfoSerializer(serializers.ModelSerializer): class Meta: model = models.UserInfo #fields = "__all__" fields = ['id','username','password','group','roles'] #表示连表的深度 depth = 1 class UserInfoView(APIView): '''用户的信息''' def get(self,request,*args,**kwargs): users = models.UserInfo.objects.all() ser = UserInfoSerializer(instance=users,many=True) ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)
访问:http://127.0.0.1:8000/api/v1/info/
4.生成url
url.py
urlpatterns = [ re_path('(?P<version>[v1|v2]+)/group/(?P<pk>\d+)/', GroupView.as_view(),name = 'gp') #序列化生成url ]
views.py
class UserInfoSerializer(serializers.ModelSerializer):
group = serializers.HyperlinkedIdentityField(view_name='gp',lookup_field='group_id',lookup_url_kwarg='pk')
class Meta: model = models.UserInfo #fields = "__all__" fields = ['id','username','password','group','roles'] #表示连表的深度 depth = 0 class UserInfoView(APIView): '''用户的信息''' def get(self,request,*args,**kwargs): users = models.UserInfo.objects.all()
#这里必须要传参数context={'request':request} ser = UserInfoSerializer(instance=users,many=True,context={'request':request})
ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret) class GroupSerializer(serializers.ModelSerializer): class Meta: model = models.UserGroup fields = "__all__"
class GroupView(APIView): def get(self,request,*args,**kwargs): pk = kwargs.get('pk') obj = models.UserGroup.objects.filter(pk=pk).first() ser = GroupSerializer(instance=obj,many=False) ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)
注意注意添加setting.py
REST_FRAMEWORK = { "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning", "ALLOWED_VERSIONS":["v1","v2"], }
访问:http://127.0.0.1:8000/api/v1/info/
可以获取到group的url