• 2、django后端:课程表、课程详细表


    1、课程表录入数据

     有些人卖接口,接口就是 数据

    1、课程表

    2张表+3张表

     

     分布式数据库分表

    数据库优化
    
    垂直分表
    
    1000w条数据,999w条数据不常看,经常查的1w条数据放在另一个表
    
    水平分表
    
    常用的列

    2、model表

    from django.db import models
    
    
    # Create your models here.
    
    class Course(models.Model):
        """
        课程表
        """
        title = models.CharField(verbose_name="课程名称", max_length=32)
        course_img = models.CharField(verbose_name="课程图片", max_length=64)
        level_choices = (
            (1, "初级"),
            (2, "中级"),
            (3, "高级")
        )
        level = models.IntegerField(verbose_name="课程难易程度", choices=level_choices, default=1)
    
        # admin中显示字段
        def __str__(self):
            return self.title
    
    
    class CourseDetail(models.Model):
        """
        课程详细
        """
        course = models.OneToOneField(to='Course', on_delete=models.CASCADE)
        slogon = models.CharField(verbose_name="口号", max_length=255)
        why = models.CharField(verbose_name="为什么要学习?", max_length=255)
        recomme_courses = models.ManyToManyField(verbose_name="推荐课程", to='Course', related_name='rc')
    
        def __str__(self):
            return '课程详细' + self.course.title
    
    
    class Chapter(models.Model):
        """
        章节
        """
        num = models.IntegerField(verbose_name='章节')
        name = models.CharField(verbose_name='章节名称', max_length=32)
        course = models.ForeignKey(verbose_name="所属课程", to='Course', on_delete=models.CASCADE)
    
        def __str__(self):
            return self.name

     生成表结构报错

    相同字段冲突

     

    2个关联字段

    __关联字段  如何查找

    反向关联字段

     

    3、admin中显示

    djano官网  https://docs.djangoproject.com/en/2.2/ref/contrib/admin/

    from django.contrib import admin
    
    from .models import Course, CourseDetail, Chapter
    # Register your models here.
    
    admin.site.register(Course)
    admin.site.register(CourseDetail)
    admin.site.register(Chapter)

    创建超级用户

     

     

    4、添加数据

    2、课程列表接口

    1、queryset与object序列化

    queryset 里面是个对象

    queryset是django的

     json可以序列化的

    2、Course表序列化,view

     序列化

     ret返回的数据

     views

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    
    from api.models import Course
    
    
    class CourseSerializer(serializers.ModelSerializer):
        class Meta:
            model = Course      # model表
            fields = '__all__'  # 所有字段
    
    
    class CourseView(APIView):
        def get(self, request, *args, **kwargs):
    
            ret = {'code': 1000, 'data': None}
    
            try:
                queryset = Course.objects.all()
                ser = CourseSerializer(instance=queryset, many=True)
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
    
            return Response(ret)

     效果

     

     

    3、choices如何序列化:level

     

     3、点击查看课程接口

    1、方式1:view接受pk

     

    from django.urls import path, include,re_path
    from api.view import course
    
    urlpatterns = [
    
        # 方式1
        path('course', course.CourseView.as_view()),
        re_path('course/(?P<pk>d+)/$', course.CourseView.as_view())  # 正则 re_path
    ]

    1.1 queryset与obj序列化

    一个对象和多个对象

     queryset 与 obj

    many=False与many=Ture

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    
    from api.models import Course
    
    
    class CourseSerializer(serializers.ModelSerializer):
        class Meta:
            model = Course      # model表
            fields = '__all__'  # 所有字段
    
    
    class CourseView(APIView):
        def get(self, request, *args, **kwargs):
    
            ret = {'code': 1000, 'data': None}
    
            try:
                pk = kwargs.get('pk')
                if pk:
                    obj = Course.objects.filter(id=pk).first()
                    print(obj)  # python全栈开发
                    print(type(obj))    # <class 'api.models.Course'>
    
                    ser = CourseSerializer(instance=obj, many=False)   # 序列化对象
                else:
                    queryset = Course.objects.all()
    
                    print(queryset)  # <QuerySet [<Course: python全栈开发>, <Course: 金融量化分析>, <Course: 智能玩具>]>
                    print(type(queryset))  # <class 'django.db.models.query.QuerySet'>
    
                    ser = CourseSerializer(instance=queryset, many=True)    # 序列化queryset
    
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
    
            return Response(ret)

    2、方式2:重写 as_view

    2.1 view的流程

    2.2 重写as_view()源码

     

    2.3 代码:url与view

     支持这种写法

    from django.urls import path, include,re_path
    from api.view import course
    
    urlpatterns = [
    
        #方式2
        path('course', course.CourseView.as_view({'get': 'list'})),
        re_path('course/(?P<pk>d+)/$', course.CourseView.as_view({'get': 'retreive'}))  # 正则 re_path
    ]

     重写as_view方法

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    
    from api.models import Course
    
    
    class CourseSerializer(serializers.ModelSerializer):
        class Meta:
            model = Course      # model表
            fields = '__all__'  # 所有字段
    
    
    # 方式2
    from rest_framework.viewsets import ModelViewSet
    
    from rest_framework.viewsets import GenericViewSet,ViewSetMixin
    
    class CourseView(ViewSetMixin, APIView):
        def list(self,request,*args,**kwargs):
            """课程列表接口"""
            ret = {'code': 1000, 'data': None}
    
            try:
                queryset = Course.objects.all()
                ser = CourseSerializer(instance=queryset, many=True)  # 序列化queryset
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
            return Response(ret)
    
        def retreive(self,request,*args,**kwargs):
            """课程详细接口"""
            ret = {'code': 1000, 'data': None}
    
            try:
                pk = kwargs.get('pk')
                obj = Course.objects.filter(id=pk).first()
                ser = CourseSerializer(instance=obj, many=False)  # 序列化对象
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
            return Response(ret)

     

    4、课程Detail接口

     

    1、所有字段

     

     

    2、depth:自动关联深度(不推荐)

    depth 自动关联 0 1 2 3  (不推荐用)

    官方建议 0~10 ,

     

    3、自定义字段source:一对一,choices

    关联字段 source

    指定字段,用sorce

    source跟数据库的某个字段绑定

     

    level显示中文:get_level_display

    自动帮我加 ()

     

      

    4、自定义method:多对多,一对多

    (1)多对多

    如何 取出推荐课程?多对多

     用all?

     

    manytomany

    自定义 2类

    序列化,想要的字段

     

    (2)反向取字段:一对多

    只能获取一个课程数据

     chapter_set.all()

     

     

     5、代码与总结

    1、总结与作业

    2、as_view分析

    # 方式2
    from rest_framework.viewsets import ModelViewSet
    
    '''
    class View:
        def as_view(cls):
            pass
    
    class APIView(View):
    
    ---------------------
    class GenericAPIView(views.APIView):
    
    class ViewSetMixin(object):
        # 重写as_view方法
        def as_view(cls, actions=None, **initkwargs):
            pass
    ---------------------
    
    class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
    class ModelViewSet(GenericViewSet)
    
    ModelViewSet
    '''

    3、序列化自定义与view

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import serializers
    
    from api.models import Course,CourseDetail
    
    
    class CourseSerializer(serializers.ModelSerializer):
        # choices
        level = serializers.CharField(source='get_level_display')
    
        class Meta:
            model = Course      # model表
            # fields = '__all__'  # 所有字段
            fields = ['id','title','course_img','level']
    
    
    class CourseDetailSerializer(serializers.ModelSerializer):
    
        # one2one/外键/choices多选的
        title = serializers.CharField(source='course.title')
        img = serializers.CharField(source='course.course_img')
        level = serializers.CharField(source='course.get_level_display')  # level显示中文
    
        # many2many/one2many
        recommends = serializers.SerializerMethodField()
        # recommends = serializers.CharField(source='recomme_courses.all')  # <QuerySet [<Course: python全栈开发>]> queryset需要序列化
        chapter = serializers.SerializerMethodField()
    
    
        class Meta:
            model = CourseDetail
            fields = ['title','img','level','recommends','chapter','course', 'slogon', 'why']
            # fields = '__all__'
            # 不推荐用
            # depth = 2   # 默认0  官方建议0~10
    
        def get_recommends(self,obj):
            """获取所有推荐的课程"""
            queryset = obj.recomme_courses.all()
            # return []
            return [{'id':row.id, 'title':row.title} for row in queryset]
    
        def get_chapter(self,obj):
            queryset = obj.course.chapter_set.all()
            return [{'id':row.id, 'name':row.name} for row in queryset]
    
    
    from rest_framework.viewsets import GenericViewSet,ViewSetMixin
    
    class CourseView(ViewSetMixin, APIView):
        def list(self,request,*args,**kwargs):
            """课程列表接口"""
            ret = {'code': 1000, 'data': None}
    
            try:
                queryset = Course.objects.all()
                ser = CourseSerializer(instance=queryset, many=True)  # 序列化queryset
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
            return Response(ret)
    
        def retreive(self,request,*args,**kwargs):
            """课程详细接口"""
            ret = {'code': 1000, 'data': None}
    
            try:
                pk = kwargs.get('pk')  # 课程id=2
                obj = CourseDetail.objects.filter(id=pk).first()   # 课程详细对象
                ser = CourseDetailSerializer(instance=obj, many=False)  # 序列化对象
                ret['data'] = ser.data
    
            except Exception as e:
                ret['code'] = 1001
                ret['error'] = '获取课程失败'
            return Response(ret)
  • 相关阅读:
    python 学习笔记7(类/对象的属性;特性,__getattr__)
    linux 误删文件恢复
    python 学习笔记6(数据库 sqlite)
    hive 函数 Cube
    边标志法填充多边形
    tolua#代码简要分析
    CocoaAsyncSocket + Protobuf 处理粘包和拆包问题
    【设计模式】适配器模式
    【设计模式】外观模式
    【操作系统】进程管理(二)
  • 原文地址:https://www.cnblogs.com/venicid/p/11608656.html
Copyright © 2020-2023  润新知