一 vue前端代码
实现的内容,通过对课程的分类,在每个不同的课程分类下显示相应的内容
我是课程
{ {ca.name}} { {course.name}}详情
序列化
class CourseCategorySerializer(serializers.ModelSerializer): class Meta: model = models.CourseDetail fields = '__all__' course_name = serializers.CharField(source='course.name') recommend_courses = serializers.SerializerMethodField() def get_recommend_courses(self, obj): return [{ 'id': course.pk, 'name': course.name} for course in obj.recommend_courses.all()]
后台代码
from django.shortcuts import renderfrom rest_framework.views import APIViewfrom rest_framework.response import Responsefrom app01 import modelsfrom app01.utils.commonUtils import MyResponsefrom app01.mySer import CourseSerializer, CourseDetailSerializerfrom rest_framework.viewsets import ViewSetMixinfrom django.core.exceptions import ObjectDoesNotExistfrom django.conf import settingsfrom rest_framework.pagination import LimitOffsetPagination# Create your views here.class Course(ViewSetMixin, APIView): def get_course(self, request, *args, **kwargs): response = MyResponse() param = request.GET.get('sub_category', None) print(param) # course_list = models.Course.objects.all() # 加分页器 # page = LimitOffsetPagination() # page.default_limit=2 # page.max_limit=3 # page_list = page.paginate_queryset(course_list,request,self) course_list = models.Course.objects.all() param = int(param) if param: if param == 0: course_list = models.Course.objects.all() else: course_list = models.Course.objects.filter(category_id=param).all() course_ser = CourseSerializer(instance=course_list, many=True) response.msg = '查询成功' response.data = course_ser.data print(response.get_dic) return Response(response.get_dic)