10步入门Django Rest framework后端接口框架
10步入门DRF框架开发
1.安装django,版本1.11.20
安装djangorestframework, 版本3.11.0
2.注册djangorestframework到setting.py文件中的APP
3.创建项目
python mamage.py startproject DRF_learn
4.创建app,并注册
python manage.py startapp DRFDemo
5. 编写数据模型
E:\DRF_learn\DRFDemo\models.py
from django.db import models # Create your models here. class Author(models.Model): """ 图书作者表 """ name = models.CharField('作者姓名', max_length=20) age = models.SmallIntegerField('年龄') def __str__(self): return self.name class Book(models.Model): """ 图书表 """ book_name = models.CharField('图书名称', max_length=100) author = models.ForeignKey(Author, related_name='author_book') publish_time = models.DateTimeField('出版时间', auto_now_add=True) def __str__(self): return self.book_name
6.数据迁移
python manage.py makemigrations
python manage.py migrate
7.编写api接口
在DRFDemo目录下新建api包,并创建author和book模块
author.py内容:
from rest_framework import generics, serializers from ..models import Author class AuthorSerializer(serializers.ModelSerializer): class Meta: model = Author fields = '__all__' class CreateAuthor(generics.CreateAPIView): """ 新增作者 """ queryset = Author.objects.all() serializer_class = AuthorSerializer
book.py内容:
from rest_framework import generics from rest_framework import serializers from ..models import Book class BookSerializer(serializers.ModelSerializer): """ Book模型的序列化器 """ # ReadOnly增加序列化时额外的字段信息(友好显示图书作者名称) author_name = serializers.ReadOnlyField(source='author.name') class Meta: model = Book fields = '__all__' class CreateBook(generics.CreateAPIView): """ 新增图书 """ # queryset = Book.objects.all() serializer_class = BookSerializer class DeleteBook(generics.DestroyAPIView): """ 删除图书 """ queryset = Book.objects.all() serializer_class = BookSerializer class UpdateBook(generics.UpdateAPIView): """ 修改图书信息 """ queryset = Book.objects.all() serializer_class = BookSerializer class BookList(generics.ListAPIView): """ 获取所有的图书列表 """ queryset = Book.objects.all() serializer_class = BookSerializer class BookDetail(generics.RetrieveAPIView): """ 获取单个图书信息 """ # 前端接口请求时只能在该queryset中查找结果,如果请求的资源不在该查询结果集中则不会返回具体的图书信息 queryset = Book.objects.filter(author_id=3) serializer_class = BookSerializer # 重写该方法,获得具体的一个对象 # def get_object(self): # obj = Book.objects.get(id=1) # return obj
总结:
以下常用的5种子类(CreateAPIView/DestroyAPIView/UpdateAPIView/ListAPIView/RetrieveAPIView)
中只有继承CreateAPIView的视图类不需要queryset属性,继承其它子类的视图类必须要有该属性,所有的视图都必须有serializer_class属性。
8. 编写路由
8.1 在DRFDemo下新增urls.py文件,里面写:
E:\DRF_learn\DRFDemo\urls.py
from django.conf.urls import url from .api import book, author urlpatterns = [ url(r'^create_book$', book.CreateBook.as_view()), url(r'^delete_book/(?P<pk>\d+)$', book.DeleteBook.as_view()), url(r'^update_book/(?P<pk>\d+)$', book.UpdateBook.as_view()), url(r'^booklist$', book.BookList.as_view()), url(r'^book/(?P<pk>\d+)$', book.BookDetail.as_view()), url(r'^create_author$', author.CreateAuthor.as_view()), ]
注意:这里的接口命名不符合Restful规范,只是为了便于区分识别
8.2 将子路由包含进项目主路由中:
E:\DRF_learn\DRF_learn\urls.py
from django.contrib import admin from django.conf.urls import url, include urlpatterns = [ url('admin/', admin.site.urls), url(r'^drf_demo/', include('DRFDemo.urls')) ]
9. 配置启动
pycharm中配置好host
修改settings.py中的ALLOWED_HOSTS参数
单击启动按钮
pycharm下方出现如下就说明启动好了
10. 访问测试:
10.1 新增作者接口
地址:http://127.0.0.1:8000/drf_demo/create_author
填写name和age,单击POST
查看数据库中,已新增
10.2 新增图书接口:
http://127.0.0.1:8000/drf_demo/create_book
数据库查看:
10.3 删除图书接口:
http://127.0.0.1:8000/drf_demo/delete_book/4
10.4 修改图书接口:
http://127.0.0.1:8000/drf_demo/update_book/3
10.5 查询单个图书接口:
http://127.0.0.1:8000/drf_demo/book/2
10.6 查询所有图书接口:
http://127.0.0.1:8000/drf_demo/booklist