DRF安装和配置
from rest_framework import serializers from . models import BookInfo
from rest_framework import serializers from .models import BookInfo, HeroInfo class BookInfoModelSerializer(serializers.ModelSerializer): """BookInfo模型类的序列化器""" class Meta: model = BookInfo fields = '__all__'
class BookInfoSerializer(serializers.ModelSerializer): """图书模型序列化器""" class Meta: model = BookInfo #将模型字段映射到序列化器 fields = '__all__' #将所有的模型字段映射到序列化器
from django.conf.urls import url from rest_framework.routers import DefaultRouter from . import views urlpatterns = [ # url(r'^books/$', views.BooksAPIView.as_view()), # url(r'^books/(?P<pk>\d+)/$', views.BookAPIView.as_view()) ] #定义式图集的路由 router = DefaultRouter() # 可以处理视图的路由器 router.register(r'books', views.BookInfoViewSet) # 向路由器中注册视图集 urlpatterns += router.urls # 将路由器中的所以路由信息追到到django的路由列表中