Django---drf序列化组件---作业
1 图书的5个接口写完(使用序列化组件)
urls.py
from django.contrib import admin from django.urls import path, re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), re_path('books/(?P<pk>\d+)', views.BookView.as_view()), path('',views.BooksView.as_view()) ]
views.py
from django.shortcuts import render from rest_framework.views import APIView,Response from app01.models import Book from app01.ser import BookSerializer from django.http.response import JsonResponse from app01.utils import MyResponse # Create your views here. class BookView(APIView): def get(self,request,pk): book = Book.objects.filter(nid=pk).first() book_ser = BookSerializer(book) return Response(book_ser.data) def put(self,request,pk): response_msg = {'status':100,'msg':'成功'} book = Book.objects.filter(nid=pk).first() boo_ser = BookSerializer(instance=book,data=request.data) if boo_ser.is_valid(): boo_ser.save() response_msg['data']=boo_ser.data else: response_msg['status'] = 101 response_msg['msg'] = '数据校检失败' response_msg['data'] = boo_ser.errors return Response(response_msg) def delete(self,request,pk): response = MyResponse() ret = Book.objects.filter(nid=pk).delete() return Response(response.get_dict) class BooksView(APIView): def get(self,request): response = MyResponse() books = Book.objects.all() book_ser = BookSerializer(books,many=True) response.data = book_ser.data return Response(response.get_dict) def post(self,request): response_msg = {'status':100,'msg':'成功'} book_ser = BookSerializer(data=request.data) if book_ser.is_valid(): book_ser.save() response_msg['data'] = book_ser.data else: response_msg['status'] = 102 response_msg['msg'] = '校检数据失败' response_msg['status'] = book_ser.errors return Response(response_msg)
modles.py
from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=64) price = models.DecimalField(decimal_places=2,max_digits=6) author = models.CharField(max_length=32) publish = models.CharField(max_length=64)
app01下ser.py创建序列化类
from rest_framework import serializers from app01.models import Book class BookSerializer(serializers.Serializer): xxd = serializers.CharField(source='nid') name = serializers.CharField() price = serializers.CharField() author = serializers.CharField() publish = serializers.CharField() def create(self, validated_data): instance = Book.objects.create(**validated_data) return instance def update(self, instance, validated_data): instance.name = validated_data.get('name') instance.price = validated_data.get('price') instance.author = validated_data.get('author') instance.publish = validated_data.get('publish') instance.save() return instance
app01下utils.py创建MyResponse类返回字典
class MyResponse(): def __init__(self): self.status = 100 self.msg = '成功' @property def get_dict(self): return self.__dict__ if __name__ == '__main__': res = MyResponse() res.status = 101 res.msg = '查询失败'
settings.py注册djangorestframwork组件
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', 'rest_framework' ]
BookSerializer的字段中添加参数source