10步入門DRF框架開發前端
1.安裝django,版本1.11.20python
安裝djangorestframework, 版本3.11.0數據庫
2.註冊djangorestframework到setting.py文件中的APPdjango
3.建立項目api
python mamage.py startproject DRF_learnapp
4.建立app,並註冊框架
python manage.py startapp DRFDemo測試
5. 編寫數據模型url
E:\DRF_learn\DRFDemo\models.pyspa
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 查詢全部圖書接口: