首先找到models文件,然后里面写上内容,自定义
class UserGroup(models.Model): title = models.CharField(max_length=32) class UserInfo(models.Model): name = models.CharField(verbose_name="姓名",max_length=255) age = models.IntegerField(verbose_name="年龄") choice = ( (1, ‘男人‘), (2, ‘女人‘), (3, ‘其他‘) ) sex = models.IntegerField(verbose_name="性别",choices=choice) address = models.CharField(verbose_name="地址",max_length=255) ug = models.ForeignKey(UserGroup,default=1,on_delete=models.CASCADE)
然后同步数据库,然后往数据库里这两张表添加内容,完毕之后回到视图函数,里面写上代码逻辑,首先定义一个序列化类
from rest_framework import serializers """ 先要创建一个类继承Serializers """ class UserSerializers(serializers.Serializer): """序列化字段和数据库必须一样""" name = serializers.CharField() age = serializers.CharField() sex = serializers.IntegerField() address = serializers.CharField() # source 来源于,就是让页面不再显示对象而是数据库的字段 ug = serializers.CharField(source=‘ug.title‘)
接下来开始写逻辑代码
from rest_framework import views from rest_framework.views import Response from app1 import models class SerializersViews(views.APIView): def get(self,request,*args,**kwargs): # user_list = models.UserInfo.objects.filter() user_list = models.UserInfo.objects.filter() # 选择序列化类,如果是多个,many=True,如果是单个,many=False sls = UserSerializers(instance=user_list,many=True) # sls = UserSerializers(instance=user_list,many=False) # sls.data 已经序列化数据库所需字段 return Response(sls.data)
完整代码如下:
from django.shortcuts import render,HttpResponse from rest_framework import views from rest_framework import serializers from rest_framework.views import Response from app1 import models # Create your views here. """ 先要创建一个类继承Serializers """ class UserSerializers(serializers.Serializer): """序列化字段和数据库必须一样""" name = serializers.CharField() age = serializers.CharField() sex = serializers.IntegerField() address = serializers.CharField() # source 来源于,就是让页面不再显示对象而是数据库的字段 ug = serializers.CharField(source=‘ug.title‘) class SerializersViews(views.APIView): def get(self,request,*args,**kwargs): # user_list = models.UserInfo.objects.filter() user_list = models.UserInfo.objects.filter() # 选择序列化类,如果是多个,many=True,如果是单个,many=False sls = UserSerializers(instance=user_list,many=True) # sls = UserSerializers(instance=user_list,many=False) # sls.data 已经序列化数据库所需字段 return Response(sls.data)
完整代码
写完之后,在你的urls里面写上
from django.contrib import admin from django.urls import path from app1 import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘Serializers/‘, views.SerializersViews.as_view()), # 添加这一行 ]
之后在你的配置文件中
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘app1.apps.App1Config‘, ‘rest_framework‘, # 配置上 ]
好了,上面完成之后,直接启动系统,网页输入url可以看到内容了,不知道为啥无法粘贴图片了,看代码吧
[ { "name": "陈晨", "age": "20", "sex": 1, "address": "河北邯郸", "ug": "A组" }, { "name": "付霞", "age": "15", "sex": 2, "address": "甘肃庆阳", "ug": "B组" } ]
这是用postman测试的,可以看到已经把数据json好了
原文地址:https://www.cnblogs.com/ArkhamKnight/p/9429695.html
时间: 2024-10-08 21:53:37