Restframework 视图组件与序列号组件的应用.
models
from django.db import models # Create your models here. class Course(models.Model): title=models.CharField(max_length=32) desc=models.CharField(max_length=32) from django.db import models # Create your models here. class Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) age=models.IntegerField() def __str__(self): return self.name class Publish(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) city=models.CharField( max_length=32) email=models.EmailField() def __str__(self): return self.name class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField( max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) # 与Publish建立一对多的关系,外键字段建立在多的一方 publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE) # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表 authors=models.ManyToManyField(to='Author',)
urls
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), #方式一 url(r"^books/$",views.BookView.as_view()), url(r"^books/(\d+)/$",views.BookViewDetail.as_view()), #方式二 url(r"^publish/$",views.PublishView.as_view()), url(r"^publish/(\d+)/$",views.PublishView.as_view()), #方式三 url(r"^authors/$", views.AuthorsView.as_view()), url(r"^authors/(\d+)/$", views.AuthorsViewDetail.as_view()), #方式四 url(r"^authors/$", views.AuthorsModelView.as_view({"get":"list","post":"create"})), url(r"^authors/(?P<pk>\d+)/$", views.AuthorsModelView.as_view({"get":"retrieve","delete":"destroy","put":"update"})), ]
serializers
from app01.models import * from rest_framework import serializers class BookSerializer(serializers.ModelSerializer): class Meta: model =Book fields="__all__" class PublishSerializer(serializers.ModelSerializer): class Meta: model=Publish fields="__all__" class AuthorSerializer(serializers.ModelSerializer): class Meta: model= Author fields="__all__"
视图:
from django.shortcuts import render # Create your views here. ###########################################BookView############################方式一 from rest_framework.views import APIView from rest_framework.response import Response from app01.models import Book,Author,Publish from app01.serializer import BookSerializer,PublishSerializer,AuthorSerializer class BookView(APIView): def get(self,request): """ 查看所有书籍 :param request: :return: """ book_list=Book.objects.all() serializer= BookSerializer(book_list,many=True) return Response(serializer.data) def post(self,request): """ 添加一条书籍 :param request: :return: """ serializer=BookSerializer(data=request.data,many=True) if serializer.is_valid(): serializer.save()#create操作. return Response(serializer.data) else: return Response(serializer.errors) class BookViewDetail(APIView): def get(self,request,pk): edit_book =Book.objects.get(pk=pk) serializer =BookSerializer(edit_book,many=False) return Response(serializer.data) def put(self,request,pk): edit_book=Book.objects.get(pk=pk) serializer =BookSerializer(data=request.data,instance=edit_book) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors) def delelte(self,request,id): edit_obj =Book.objects.get(pk=id).delete() return Response("11") ##################################PublishView#######################################方式二 from rest_framework import generics from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin class PublishView(ListModelMixin,CreateModelMixin,generics.GenericAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializer def get(self,request,*args,**kwargs): return self.list(request,*args,**kwargs) def post(self,request,*args,**kwargs): return self.create(request,*args,**kwargs) class PublishViewDetail(UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin,generics.GenericAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializer def get(self,request,*args,**kwargs): return self.retrieve(request,*args,**kwargs) def put(self,request,*args,**kwargs): return self.update(request,*args,**kwargs) def delete(self,request,*args,**kwargs): return self.destroy(request,*args,**kwargs) ##############################################AuthorView##################################方式三 class AuthorsView(generics.ListCreateAPIView): queryset = Author.objects.all() print(queryset) serializer_class = AuthorSerializer print(serializer_class) class AuthorsViewDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Author.objects.all() print(queryset) serializer_class = AuthorSerializer print(serializer_class) #############################################AuthorView#ModelView###########################方式四 from rest_framework.viewsets import ModelViewSet class AuthorsModelView(ModelViewSet): queryset = Author.objects.all() serializer_class = AuthorSerializer