lf 前后端分离 (2) 课程数据获取,Serializer的返回

一.关于课程数据的返回

在进行前后端分离时,会通过def 进行前后端传值,

本质上遵循rest 网址规范  增删改查查 get,post,put,del get(\d+)

1.在从数据库获取数据后,进行def 发送ajax数据,

通过from rest_framework import serializers

快速获取数据,在 class CourseSerializer(serializers.ModelSerializer)

level = serializers.CharField(source="get_level_display")为获取的一对多的关联数据库的具体字段,不加source会直接显示_id

在进行choice字段时,为了显示文字,采用
level = serializers.CharField(source="course.get_level_display")

在进行多对多字段时,需要自定义方法,遵循

recommend_coursesinfo = serializers.SerializerMethodField()
通过循环,所关联的全部字段,打印出要获取的值,注意,当字段函数与返回值同名时,会覆盖

 1 from django.shortcuts import render
 2 from rest_framework import serializers
 3 
 4 from app01.models import *
 5 
 6 
 7 class CourseSerializer(serializers.ModelSerializer):
 8     class Meta:
 9         model = Course
10         fields = "__all__"
11 
12     level = serializers.CharField(source="get_level_display")
13     coursedetail_id = serializers.CharField(source="coursedetail.pk")
14 
15 
16 class CourseDetailSerializer(serializers.ModelSerializer):
17     class Meta:
18         model = CourseDetail
19         fields = "__all__"
20 
21     name = serializers.CharField(source="course.name")
22     prices = serializers.SerializerMethodField()
23     brief = serializers.CharField(source="course.brief")
24     study_all_time = serializers.StringRelatedField(source='hours')
25     level = serializers.CharField(source="course.get_level_display")
26     teacher = serializers.SerializerMethodField()
27     is_online = serializers.CharField(source="course.get_status_display")
28     recommend_coursesinfo = serializers.SerializerMethodField()
29 
30     def get_prices(self,instance):
31         return [{"price": obj.price,
32                  "valid_period": obj.valid_period,
33                  "valid_period_text": obj.get_valid_period_display(),
34                  "id": obj.pk,
35                  }for obj in instance.course.price_policy.all()]
36 
37     def get_teacher(self, instance):
38         return [{"name": obj.name,
39                  "image": obj.image}for obj in instance.teachers.all()]
40 
41     def get_recommend_coursesinfo(self, instance):
42         return [{"name": obj.name,
43                  "pk": obj.pk} for obj in instance.recommend_courses.all()]
serializers

并且返回的数据需要与前端vue匹配,跨

 1 from django.shortcuts import render
 2 
 3 from app01 import models
 4 from app01.utils.auth import LoginAuth
 5 
 6 from rest_framework.response import Response
 7 from rest_framework.views import APIView
 8 
 9 from serializer.views import CourseSerializer, CourseDetailSerializer
10 
11 
12 class CourseViews(APIView):
13     authentication_classes = [LoginAuth]
14 
15     def get(self, request):
16         course = models.Course.objects.all()
17         data = CourseSerializer(course, many=True)
18         res = {"error_no": 0, "data": data.data}
19         return Response(res)
20 
21 
22 class CourseDetailViews(APIView,):
23 
24     authentication_classes = [LoginAuth]
25 
26     def get(self, request, pk):
27         coursedetail = models.CourseDetail.objects.get(pk=pk)
28         data = CourseDetailSerializer(coursedetail, many=False)
29         res = {"error_no": 0, "data": data.data}
30         return Response(res)
course

 

其他详见

 

posted @ 2018-12-20 10:16  逆欢  阅读(332)  评论(0编辑  收藏  举报