标签:__ serializers 查询 views 多字段 course import 序列化 class
在 serializers.py中添加序列化
# -*- coding: utf-8 -*- from rest_framework import serializers from course.models import * class CourseTagSerializer(serializers.ModelSerializer): class Meta: model=CourseTag fields='__all__' class CourseTypeSerializer(serializers.ModelSerializer): class Meta: model=CourseType fields='__all__' class CourseSerializer(serializers.ModelSerializer): class Meta: model=Course fields = '__all__' class SectionsSerializer(serializers.ModelSerializer): class Meta: model=Sections fields = '__all__' class ChaptersSerializer(serializers.ModelSerializer): sections=SectionsSerializer(many=True) class Meta: model=Chapters fields = '__all__' class CourssesDeepSerializer(CourseSerializer): chapters=CourseSerializer(many=True)course/serialiers
在course/views.py添加视图函数
from django.shortcuts import render # Create your views here. from rest_framework import viewsets from rest_framework.pagination import PageNumberPagination from course.models import * from course.serializers import * class PageNum(PageNumberPagination): page_size_query_param = 'page-size' class CourseTypeViewSet(viewsets.ModelViewSet): queryset = CourseType.objects.all() serializer_class=CourseTypeSerializer class CourseTagViewSet(viewsets.ModelViewSet): queryset = CourseTag.objects.all() serializer_class=CourseTagSerializer class CourseViewSet(viewsets.ModelViewSet): queryset=Course.objects.all() serializer_class=CourseSerializer def get_serializer_class(self): if self.action =='list': return CourseSerializer else: return CourssesDeepSerializer class SectionsViewSet(viewsets.ModelViewSet): queryset = Sections.objects.all() serializer_class=SectionsSerializer class ChaptersViewSet(viewsets.ModelViewSet): queryset = Chapters.objects.all() serializer_class = ChaptersSerializercourse/views.py
在子路由添加路由
# -*- coding: utf-8 -*- from django.urls import path,re_path from rest_framework.routers import DefaultRouter from . import views router=DefaultRouter() router.register(r'type',views.CourseTypeViewSet) router.register(r'tag',views.CourseTagViewSet) router.register(r'course',views.CourseViewSet) router.register(r'sections',views.SectionsViewSet) router.register(r'chapters',views.ChaptersViewSet) urlpatterns = [ ] urlpatterns+=router.urls
在syl/urls.py中添加跟路由
from django.contrib import admin from django.urls import path,include from django.conf.urls.static import static from syl import settings urlpatterns = [ path('course/', include('course.urls')), ] urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
标签:__,serializers,查询,views,多字段,course,import,序列化,class
来源: https://www.cnblogs.com/daiju123/p/13806590.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。