Django - rest - framework - 上

1、快速實例

http://www.django-rest-framework.org/tutorial/quickstart/#quickstarthtml

http://www.cnblogs.com/yuanchenqi/articles/8719520.html正則表達式

簡介:

restframework(Django)  app
pip install django
pip install djangorestframework

(先後端分離 後端返回 json 數據)

----針對數據:json

---- 一切皆是資源,操做只是請求方式

----book表增刪改查
/books/ books
/books/add/ addbook
/books/(\d+)/change/ changebook
/books/(\d+)/delete/ delbook

----book表增刪改查 url裏面不能出現動詞!!
/books/ -----get books ----- 返回當前全部數據
/books/ -----post books ----- 返回提交數據

/books/(\d+)-----get bookdetail ----- 返回當前查看的單條數據
/books/(\d+)-----put bookdetail ----- 返回更新數據
/books/(\d+)-----delete bookdetail ----- 返回空

應用:
from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^book/', views.Book.as_view()),
    url(r'^bookdetail/(\d+)', views.BookDetail.as_view()),

]

---------------------------------------------------------------

from django.shortcuts import render,redirect,HttpResponse

# Create your views here.

from django.views import View

class Book(View):

    def get(self,request):
        return HttpResponse("get all...")    # 查看全部書籍

    def post(self,request):
        return HttpResponse("post all...")   # 添加書籍


class BookDetail(View):

    def get(self,request,pk):
        return HttpResponse("get ..."+ pk)   # 查看某本書籍

    def put(self,request,pk):
        return HttpResponse("put ..." + pk)  # 查看某本書籍

    def post(self,request,pk):
        return HttpResponse("post ..." + pk) # 添加某本書籍

    def delete(self,request,pk):
        return HttpResponse("delete ..." + pk) # 刪除某本書籍

 

2、序列化

url(r"^publishes/", views.PublishView.as_view())

 

方式一 list()

class PublishView(View):
    def get(self,request):
        # 方式一:
        publish_list = list(Publish.objects.all().values("name","email"))
        # {'name': '蘋果出版社', 'email': '123@qq.com'}{'name': '橘子出版社', 'email': '456@qq.com'}
        return HttpResponse(publish_list)

 

方式二 model_to_dict(obj)

class PublishView(View):
    def get(self,request):
        # 方式二:
        from django.forms.models import model_to_dict
        publish_list = Publish.objects.all()
        temp = []
        for obj in publish_list:
            # temp.append({
            #     "name":obj.name,
            #     "email":obj.email
            # })
            temp.append(model_to_dict(obj))
        print(temp, type(temp))
        # [{'name': '蘋果出版社', 'email': '123@qq.com'}, {'name': '橘子出版社', 'email': '456@qq.com'}] <class 'list'>
        # [{'id': 1, 'name': '蘋果出版社', 'email': '123@qq.com'}, {'id': 2, 'name': '橘子出版社', 'email': '456@qq.com'}] <class 'list'>

        return HttpResponse(temp)

 

方式三 serializers.serialize("json",publish_list)

class PublishView(View):
    def get(self,request):
        # 方式三
        from django.core import serializers
        publish_list = Publish.objects.all()
        ret = serializers.serialize("json",publish_list)
        """
        [
            {
                "model": "app01.publish",
                "pk": 1,
                "fields": {
                    "name": "蘋果出版社",
                    "email": "123@qq.com"
                }
            },
            {
                "model": "app01.publish",
                "pk": 2,
                "fields": {
                    "name": "橘子出版社",
                    "email": "456@qq.com"
                }
            }
        ]
        """
        return HttpResponse(ret)

 

方式四(推薦)(rest_framework  serializers)

序列化一個queryset:  PublishSerializers(publish_list, many=True)
序列化一個對象: PublishSerializers(model_obj)

from rest_framework import serializers

class PublishSerializers(serializers.Serializer):
    name = serializers.CharField()
    email = serializers.EmailField()


class PublishView(View):
    def get(self,request):
        # 方式四(推薦)
        publish_list = Publish.objects.all()
        ret = PublishSerializers(publish_list, many=True) # queryset
        # print(ret.data)
        # print(type(ret.data))
        # [OrderedDict([('name', '蘋果出版社'), ('email', '123@qq.com')]),
        # OrderedDict([('name', '橘子出版社'), ('email', '456@qq.com')])]
        # <class 'rest_framework.utils.serializer_helpers.ReturnList'>
        """
        >>>dict([("name","橘子出版社"),("email","456@qq.com")])
        {'name': '橘子出版社', 'email': '456@qq.com'}
        """

        return HttpResponse(ret.data)

 

3、rest_framework (APIView)

models.py

from django.db import models

# Create your models here.

class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.IntegerField()
    pub_date = models.DateField()
    publish = models.ForeignKey("Publish")
    authors = models.ManyToManyField("Author")


class Publish(models.Model):
    name = models.CharField(max_length=32)
    email = models.EmailField()

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()

    def __str__(self):
        return self.name

 

取數據

get:django

  request._request ( 原request )json

  request.GET == request._request.GET  後端

post:瀏覽器

  request.data ( 獲得 json 數據,也可獲得 urlencode 數據)服務器

    都是 rest_framework  APIView 的功勞,restful

  若是使用 View , request.post( 只能獲得 urlencode 的數據,不能獲得 json 數據,只能從 request.body 中取) app

 

from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from .models import *
from rest_framework.views import APIView, Response
from rest_framework import serializers


class PublishSerializers(serializers.Serializer):
    name = serializers.CharField()
    email = serializers.EmailField()


class PublishView(APIView):
    def get(self,request):
        # 取數據 APIView
        publish_obj = Publish.objects.all()
        ret = PublishSerializers(publish_obj,many=True)
        print(ret.data)
        # [OrderedDict([('name', '蘋果出版社'), ('email', '123@qq.com')]),
        # OrderedDict([('name', '橘子出版社'), ('email', '456@qq.com')])]

        print(request.GET)  # <QueryDict: {'a': ['1'], 'b': ['2']}>
        print("data",request.data) #  # 只處理 post
        print(request._request.GET) # <QueryDict: {'a': ['1'], 'b': ['2']}>

        return HttpResponse(ret.data)
        # return Response(ret.data)

    def post(self,request):
        # 取數據  View              (原生request支持得操做)
        # print("post",request.POST) # <QueryDict: {'a': ['1'], 'b': ['2']}>
        # print("body",request.body) #  b'a=1&b=2'

        # 發的是json數據  View (不處理json數據,只解析urlencode)   (原生request支持得操做)
        # print("post", request.POST)  # <QueryDict: {}>
        # print("body", request.body)  # b'{"name":"yuan","age":12}'

        # print(type(request)) # <class 'django.core.handlers.wsgi.WSGIRequest'>
        # from django.core.handlers.wsgi import WSGIRequest

        # APIView    (新得request支持得操做)
        print(type(request)) # <class 'rest_framework.request.Request'>
        from rest_framework.request import Request
        print(request._request) # <WSGIRequest: POST '/publishes/'>
        print("data",request.data)  # {'name': 'yuan', 'age': 12}
        print("type",type(request.data)) #  <class 'dict'>
        return HttpResponse('POST')

 

4、rest_framework (Response)

""" 響應器
針對 不一樣得訪問者 瀏覽器 postman 作不一樣得處理
只有瀏覽器,用戶 會給一個頁面!!

幾種請求方式
get post
get put delete

"""前後端分離

Response: 

        

 

配置以後:瀏覽器纔可訪問; rest_framework 是django的一個app

可發 get / post 請求 !!

 

postman:  可模擬瀏覽器發送

  get post put delete ...

 

 

   

 

from .models import *
from rest_framework.views import APIView, Response
from rest_framework import serializers


class PublishSerializers(serializers.Serializer):
    name = serializers.CharField()
    email = serializers.EmailField()


class PublishView(APIView):
    def get(self,request):
        publish_obj = Publish.objects.all()
        # model_obj = Publish.objects.all().first()
        ret = PublishSerializers(publish_obj,many=True)  # queryset
        # ret = PublishSerializers(model_obj)  # obj 
        return Response(ret.data)

    def post(self,request):
        print("data",request.data)  # {'name': 'yuan', 'age': 12}
        return Response(request.data)

 

5、rest_framework (serializers.Serializer)

一對多:

  publish = serializers.CharField()       # str(obj) 

  publish = serializers.CharField(source='publish.email')

 

多對多:  

# authors = serializers.CharField(source="authors.all") 

authors = serializers.SerializerMethodField()
def get_authors(self,book_obj):
  temp = []
  for obj in book_obj.authors.all():
    temp.append(obj.name)
  return temp

 

   

 

from .models import *
from rest_framework.views import APIView, Response
from rest_framework import serializers


class BookSerializers(serializers.Serializer):
    title = serializers.CharField(max_length=32)
    price = serializers.IntegerField()
    pub_date = serializers.DateField()
    publish = serializers.CharField(source='publish.email')
    # authors = serializers.CharField(source="authors.all")

    authors = serializers.SerializerMethodField()
    def get_authors(self,book_obj):
        temp = []
        for obj in book_obj.authors.all():
            temp.append(obj.name)
        return temp

class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookSerializers(book_list,many=True)
        return Response(ret.data)

    def post(self, request):
        pass

"""
序列化的過程:
    BookSerializers(book_list,many=True)
    temp = []
    for obj in book_list:
        "title":obj.title,
        "price":obj.price,
        "pub_date":obj.pub_date,
        "publish":str(obj.publish),
        #"authors":obj.authors.all,
        "authors":get_authors(obj)  
"""



class PublishSerializers(serializers.Serializer):
    name = serializers.CharField()
    email = serializers.EmailField()


class PublishView(APIView):
    def get(self,request):
        publish_obj = Publish.objects.all()
        # model_obj = Publish.objects.all().first()
        ret = PublishSerializers(publish_obj,many=True)
        # ret = PublishSerializers(model_obj)
        return Response(ret.data)

    def post(self,request):
        print("data",request.data)  # {'name': 'yuan', 'age': 12}
        return Response(request.data)


-----------------------------------------

from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r"^publishes/", views.PublishView.as_view()),
    url(r"^books/", views.BookView.as_view())
]

 

from django.db import models

# Create your models here.

class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.IntegerField()
    pub_date = models.DateField()
    publish = models.ForeignKey("Publish")
    authors = models.ManyToManyField("Author")

    def __str__(self):
        return self.title

class Publish(models.Model):
    name = models.CharField(max_length=32)
    email = models.EmailField()

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()

    def __str__(self):
        return self.name
models.py

 

6、rest_framework (serializers.ModelSerializer)

fields = "__all__"  

固然了,可定製一對多,多對多:

  publish = serializers.CharField(source="publish.email")

  authors = serializers.SerializerMethodField()

    ... ...

 

 

get

class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    # publish = serializers.CharField()  # str(obj)
    publish = serializers.CharField(source="publish.email")

    # authors = serializers.CharField(source="authors.all")
    authors = serializers.SerializerMethodField()
    def get_authors(self, book_obj):
        temp = []
        for obj in book_obj.authors.all():
            temp.append(obj.name)
        return temp


class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookModelSerializers(book_list,many=True)
        return Response(ret.data)

 

post 

bms = BookModelSerializers(data = request.data)

bms.is_valid():                  # 可校驗

bms.save()                       # create操做

bms.validated_date          # 驗證後得數據

bms.errors                       # 錯誤信息

def create(self, validated_data): pass   # 重寫create操做

 

# post 發送得數據

{"title":"金瓶mei","price":88,"pub_date":"2018-12-12","publish":2,"authors":[1,2]}

 

from .models import *
from rest_framework.views import APIView, Response
from rest_framework import serializers


# class BookSerializers(serializers.Serializer):
#     title = serializers.CharField(max_length=32)
#     price = serializers.IntegerField()
#     pub_date = serializers.DateField()
#     publish = serializers.CharField(source='publish.email')
#     # authors = serializers.CharField(source="authors.all")
#
#     authors = serializers.SerializerMethodField()
#     def get_authors(self,book_obj):
#         temp = []
#         for obj in book_obj.authors.all():
#             temp.append(obj.name)
#         return temp



class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    # publish = serializers.CharField()
    # publish = serializers.CharField(source="publish.email",read_only=True)

    # authors = serializers.CharField(source="authors.all")
    # authors = serializers.SerializerMethodField()
    # def get_authors(self, book_obj):
    #     temp = []
    #     for obj in book_obj.authors.all():
    #         temp.append(obj.name)
    #     return temp

    # def create(self, validated_data):
    #     print("validated_data--->",validated_data)
    #     book = Book.objects.create(title=validated_data['title'],price=validated_data["price"],
    #                                pub_date=validated_data["pub_date"],publish=validated_data["publish"])
    #     book.authors.add(*validated_data["authors"])
    #
    #     return book

class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookModelSerializers(book_list,many=True)
        return Response(ret.data)

    def post(self, request):
        # post請求數據
        bms = BookModelSerializers(data=request.data)
        if bms.is_valid():
            bms.save() # create()  能夠重寫create 固然也能夠不寫
            # print("data:-->",bms.validated_data)
            # OrderedDict([('title', '金瓶mei11'), ('price', 88),
            # ('pub_date', datetime.date(2018, 12, 12)),
            # ('publish', <Publish: Publish object>),
            #  ('authors', [<Author: alex>, <Author: egon>])])

            return Response(bms.data)
        else:
            return Response(bms.errors)

 

簡潔方法:

authors = validated_data.pop("authors")

obj = Book.objects.create(**validated_data)


obj.authors.add(*authors)

 

from .models import *
from rest_framework.views import APIView, Response
from rest_framework import serializers

class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    def create(self, validated_data):
        authors = validated_data.pop("authors")
        print(authors)  # [<Author: alex>, <Author: egon>]
        obj = Book.objects.create(**validated_data)
        obj.authors.add(*authors)
        return obj

class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookModelSerializers(book_list,many=True)
        return Response(ret.data)

    def post(self, request):
        bms = BookModelSerializers(data=request.data,many=False)
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)

 

7、單條數據(get put delete)

get

  bms = BookModelSerializers(book)

put

  bms = BookModelSerializers(book,data=request.data)

delete

  Book.objects.filter(pk=book_id).delete()

 

 

url(r'^books/(\d+)/$',views.BookDetailView.as_view())

---------------------------------------------------

class BookDetailView(APIView):
    def get(self,request,book_id):
        book = Book.objects.filter(pk=book_id).first()
        # 序列化
        bms = BookModelSerializers(book)
        return Response(bms.data)

    def put(self,request,book_id):
        book = Book.objects.filter(pk=book_id).first()
        bms = BookModelSerializers(book,data=request.data)
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)

    def delete(self,reqeust,book_id):
        Book.objects.filter(pk=book_id).delete()
        return Response()

-------------------------------------------------

class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    # def create(self, validated_data):
    #    authors = validated_data.pop("authors")
    #    obj = Book.objects.create(**validated_data)
    #    obj.authors.add(*authors)
    #    return obj

 

from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^publishes/', views.PublishView.as_view()),
    url(r"^books/$", views.BookView.as_view()),
    url(r'^books/(\d+)/$',views.BookDetailView.as_view())

]
urls.py
from rest_framework.views import APIView, Response

from app01.serializers import *


class PublishView(APIView):
    def get(self, request):
        publish_list = Publish.objects.all()
        ret = PublishSerializers(publish_list, many=True)
        return Response(ret.data)

    def post(self, request):
        ps = PublishSerializers(data=request.data)
        if ps.is_valid():
            ps.save()
            return Response(ps.data)
        else:
            return Response(ps.errors)


class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookModelSerializers(book_list, many=True)
        return Response(ret.data)

    def post(self, request):
        bms = BookModelSerializers(data=request.data, many=False)
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)


class BookDetailView(APIView):
    def get(self,request,book_id):
        book = Book.objects.filter(pk=book_id).first()
        # 序列化
        bms = BookModelSerializers(book)
        return Response(bms.data)

    def put(self,request,book_id):
        book = Book.objects.filter(pk=book_id).first()
        bms = BookModelSerializers(book,data=request.data)
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)

    def delete(self,reqeust,book_id):
        Book.objects.filter(pk=book_id).delete()
        return Response()
views.py
# -*- coding:utf-8 -*-
from .models import *
from rest_framework import serializers


class PublishSerializers(serializers.Serializer):
    name = serializers.CharField()
    email = serializers.CharField()

    def create(self, validated_data):
        obj = Publish.objects.create(**validated_data)
        return obj


class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    def create(self, validated_data):
        authors = validated_data.pop("authors")
        obj = Book.objects.create(**validated_data)
        obj.authors.add(*authors)
        return obj
serializers.py

 

8、HyperlinkedIdentityField

配置:

  url(r'^publishes/(?P<pk>\d+)/$', views.PublishDetailView.as_view(),name="detail_publish"),

  ret = BookModelSerializers(book_list, many=True,context={"request":request})

# 顯示超連接
publish = serializers.HyperlinkedIdentityField(
  view_name="detail_publish",     # 別名 含正則表達式
  lookup_field="publish_id",
  lookup_url_kwarg="pk",
)

 

 

from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^publishes/$', views.PublishView.as_view(),name="publish"),
    url(r'^publishes/(?P<pk>\d+)/$', views.PublishDetailView.as_view(),name="detail_publish"),
    url(r"^books/$", views.BookView.as_view(),name="books"),
    url(r'^books/(?P<pk>\d+)/$',views.BookDetailView.as_view(),name="detail_book")

]
urls.py
from rest_framework.views import APIView, Response

from app01.serializers import *


class PublishView(APIView):
    def get(self, request):
        publish_list = Publish.objects.all()
        ret = PublishModelSerializers(publish_list, many=True)
        return Response(ret.data)

    def post(self, request):
        ps = PublishModelSerializers(data=request.data)
        if ps.is_valid():
            ps.save()
            return Response(ps.data)
        else:
            return Response(ps.errors)

class PublishDetailView(APIView):
    def get(self,request,pk):
        publish = Publish.objects.filter(pk=pk).first()
        ps = PublishModelSerializers(publish)
        return Response(ps.data)

    def put(self,request,pk):
        publish = Publish.objects.filter(pk=pk).first()
        ps = PublishModelSerializers(publish,data=request.data)
        if ps.is_valid():
            ps.save()
            return Response(ps.data)
        else:
            return Response(ps.errors)

    def delete(self,request,pk):
        Publish.objects.filter(pk=pk).delete()
        return Response()
    
class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        ret = BookModelSerializers(book_list, many=True,context={"request":request})
        return Response(ret.data)

    def post(self, request):
        bms = BookModelSerializers(data=request.data, many=False,context={"request":request})
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)


class BookDetailView(APIView):
    def get(self,request,pk):
        book = Book.objects.filter(pk=pk).first()
        # 序列化
        bms = BookModelSerializers(book,context={"request":request})
        return Response(bms.data)

    def put(self,request,pk):
        book = Book.objects.filter(pk=pk).first()
        bms = BookModelSerializers(book,data=request.data,context={"request":request})
        if bms.is_valid():
            bms.save()
            return Response(bms.data)
        else:
            return Response(bms.errors)

    def delete(self,reqeust,pk):
        Book.objects.filter(pk=pk).delete()
        return Response()
views.py
# -*- coding:utf-8 -*-
from .models import *
from rest_framework import serializers


class PublishModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Publish
        fields = "__all__"


class BookModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = "__all__"

    # 顯示超連接
    publish = serializers.HyperlinkedIdentityField(
        view_name="detail_publish", # 別名 含正則表達式
        lookup_field="publish_id",
        lookup_url_kwarg="pk",
    )

    # publish = serializers.CharField(source="publish.pk")
    # publish = serializers.CharField()
    # authors = serializers.CharField(source="authors.all")
    # authors = serializers.SerializerMethodField()
    # def get_authors(self,obj):
    #     temp = []
    #     for obj in obj.authors.all():
    #         temp.append(obj.name)
    #     return temp
    #
    # def create(self, validated_data):
    #     print('--->',validated_data)
    #     book = Book.objects.create(title=validated_data["title"],price=validated_data['price'],
    #                         pub_date=validated_data['pub_date'],publish_id=validated_data['publish']['pk'])
    #     book.authors.add(*validated_data['authors'])
    #
    #     return book
serializers.py

 

restful協議

     ----  一切皆是資源,操做只是請求方式

     ----book表增刪改查
         /books/                 books
         /books/add/             addbook
         /books/(\d+)/change/    changebook
         /books/(\d+)/delete/    delbook

    ----book表增刪改查  url裏面不能出現動詞!!
         /books/     -----get            books      -----  返回當前全部數據
         /books/     -----post           books      -----  返回提交數據

         /books/(\d+)-----get            bookdetail -----  返回當前查看的單條數據
         /books/(\d+)-----put            bookdetail -----  返回更新數據
         /books/(\d+)-----delete         bookdetail -----  返回空


    http://www.cnblogs.com/yuanchenqi/articles/8719520.html
    http://www.django-rest-framework.org/tutorial/quickstart/#quickstart


         class  Books(View):
              def get(self,request):
                  pass  # 查看全部書籍

              def post(self,request):
                  pass  # 添加書籍


         class  BooksDetail(View):
              def get(self,request,id):
                  pass  # 查看具體書籍

              def put(self,request,id):
                  pass  # 更新某本書籍

              def delete(self,request,id):
                  pass  # 刪除某本書籍

http://www.django-rest-framework.org/tutorial/quickstart/#quickstart
restframework(Django)  app
pip install django
pip install djangorestframework

    ----針對數據:json

    (1)Django的原生request:

          瀏覽器   -------------  服務器

          "GET url?a=1&b=2 http/1.1\r\user_agent:Google\r\ncontentType:urlencoded\r\n\r\n"
          "POST url http/1.1\r\user_agent:Google\r\ncontentType:urlencoded\r\n\r\na=1&b=2"

          request.body: a=1&b=2
          request.POST:
                       if contentType:urlencoded:
                             a=1&b=2----->{"a":1,"b":2}

    (2)restframework 下的APIView:

    (3)
    class PublishSerializers(serializers.Serializer):
            name=serializers.CharField()
            email=serializers.CharField()

        PublishSerializers(queryset,many=true)
        PublishSerializers(model_obj)


---------------------------

    總結:
        1 reuqest類----源碼

        2 restframework 下的APIView--源碼

          url(r'^books/$', views.BookView.as_view(),name="books")#  View下的view

          books/一旦被訪問: view(request) ------APIView: dispatch()

        3 def dispatch():

              構建request對象
              self.request=Request(request)
              self.request._request
              self.request.GET  # get
              self.request.data # POST  PUT

              分發----if get請求:
                    if request.method.lower() in self.http_method_names:
                        handler = getattr(self, request.method.lower(),
                                         self.http_method_not_allowed)
                    else:
                        handler = self.http_method_not_allowed

                    response = handler(request, *args, **kwargs) # self.get(request, *args, **kwargs)

                    return response

        4 序列化類
            # from django.core import serializers
            # ret=serializers.serialize("json",publish_list)

            restframework下的序列類  BookModelSerializers
                將queryset或者model對象序列成一json數據
                    bs=BookModelSerializers(book_list,many=True,context={'request': request})
                    bs=BookModelSerializers(book,context={'request': request})

                還能夠作校驗數據,json-------》queryset/model-->記錄

                    bs=BookModelSerializers(data=request.data)
                    if bs.is_valid():
                        print(bs.validated_data)
                        bs.save() # 重寫create方法
        5 操做數據:

            以Book表爲例
                class BookView(APIView):
                    # 查看全部書籍
                    def get(self,request):
                        book_list=Book.objects.all()
                        bs=BookModelSerializers(book_list,many=True,context={'request': request})
                        return Response(bs.data)

                    # 添加一本書籍
                    def post(self,request):
                        # post請求的數據
                        bs=BookModelSerializers(data=request.data)
                        if bs.is_valid():
                            print(bs.validated_data)
                            bs.save()# create方法
                            return Response(bs.data)
                        else:
                            return Response(bs.errors)

                class BookDetailView(APIView):
                    # 查看一本書籍
                    def get(self,request,id):

                        book=Book.objects.filter(pk=id).first()
                        bs=BookModelSerializers(book,context={'request': request})
                        return Response(bs.data)
                    # 更新一本書籍
                    def put(self,request,id):
                        book=Book.objects.filter(pk=id).first()
                        bs=BookModelSerializers(book,data=request.data)
                        if bs.is_valid():
                            bs.save()
                            return Response(bs.data)
                        else:
                            return Response(bs.errors)
                    # 刪除某一本書籍
                    def delete(self,request,id):
                        Book.objects.filter(pk=id).delete()

                        return Response()

restframework
    1 APIView
    2 序列組件
    3 視圖、
    4 組件(認證權限頻率)
    5 數據解析器
    6 分頁和Response
筆記
相關文章
相關標籤/搜索