2019-08-29

前后端分离 知识点

前后端分离
对表单进行增删该查

views

from rest_framework import mixins  # 混合视图
from rest_framework import generics  # 类视图
from rest_framework.response import Response  # 响应
from rest_framework import status   # 状态
from .models import Publisher   # 模型
from .serializers import Publish  # 序列化

"""
#     获取出版社信息  ,创建一个新的出版社
#     """
class PublisherList(generics.ListCreateAPIView):
    queryset = Publisher.objects.all()  #数据源  queryset一定要这么写
    serializer_class = Publish  # 序列化的类




class PublisherDetail(generics.RetrieveUpdateDestroyAPIView):  # 视图最基本的功能
    queryset = Publisher.objects.all()
    serializer_class = Publish

models

from django.db import models

# Create your models here.


class Publisher(models.Model):
    name = models.CharField(max_length=32, verbose_name='名称', unique=True)
    address = models.CharField(max_length=128, verbose_name='地址')

    def __str__(self):
        return self.name

    class Meta:
        db_table = "pub_db"
        verbose_name = '出版社'
        verbose_name_plural = verbose_name

创建一个系列化的文件夹 serializers .py

from rest_framework import serializers   # DRF框架的方式
from .models import Publisher



class Publish(serializers.ModelSerializer):
    class Meta:
        model = Publisher  #我们要使用的mox类
        # 需要序列化  和   反序列化的字段
        fields = "__all__"   #取出所有字段

你可能感兴趣的:(2019-08-29)