Python Version: 2.7.10
Django Version: 1.9
REST framework 3.3.2
安装
pip install django
pip install djangorestframework
settings.py INSTALLED_APPS中引入rest_framework
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', 'rest_framework', ]
创建model, 修改mdels.py
class Book(models.Model): name=models.CharField(max_length=100) title=models.CharField(max_length=100) author=models.CharField(max_length=100)
创建serializers.py
from app01.models import * from rest_framework import serializers class BookSerializer(serializers.Serializer): name = serializers.CharField(max_length=100) title = serializers.CharField(max_length=100) author = serializers.CharField(max_length=100) def create(self, validated_data): return Book(**validated_data) def update(self, instance, validated_data): instance.email = validated_data.get('name', instance.name) instance.content = validated_data.get('title', instance.title) instance.created = validated_data.get('author', instance.author) return instance
restore_object() 3.0以上版本不支持已经被create()和update()方法代替,源码如下:
( 'Serializer `%s.%s` has old-style version 2 `.restore_object()` ' 'that is no longer compatible with REST framework 3. ' 'Use the new-style `.create()` and `.update()` methods instead.' % (self.__class__.__module__, self.__class__.__name__) )
创建基于类和函数的视图
from django.shortcuts import render_to_response from django.http import HttpResponse from app01.forms import * from rest_framework.views import APIView from app01.serializers import BookSerializer from app01.models import * from rest_framework.response import Response from rest_framework import generics from rest_framework import status # Create your views here. class BookList(APIView): def get(self,request,format=None): books=Book.objects.all() ser=BookSerializer(books, many=True) return Response(ser.data) def post(self,request,format=None): ser=BookSerializer(data = request.data) if ser.is_valid(): ser.create(ser.validated_data).save() return Response(ser.data,status = status.HTTP_201_CREATED) return Response(ser.errors) class BookDetail(APIView): def get(self,request,num,format=None): b=Book.objects.get(id=num) ser=BookSerializer(b) return Response(ser.data) class GenericBookList(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializer def post(self,request,format=None): ser=BookSerializer(data = request.data) if ser.is_valid(): ser.create(ser.validated_data).save() return Response(ser.data,status = status.HTTP_201_CREATED) return Response(ser.errors) def index(request): return render_to_response('index.html') def hello(request): if request.method == 'POST': form = BookForms(request.POST) if form.is_valid(): data = form.cleaned_data title = data["title"] return HttpResponse(title) form = BookForms() return render_to_response('1.html', {'form':form})
urls.py配置
from django.conf.urls import url from django.contrib import admin from app01.views import * urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', index), url(r'^hello/$', hello), url(r'^book/$', BookList.as_view()), url(r'^genericbook/$', GenericBookList.as_view()), url(r'^book/(\d+)', BookDetail.as_view()), ]
APIView
generic通用视图
RESTful Request:GET/PUT/DELETE/POST/HEAD/OPTIONS
POST请求被定义为创建“从属资源”(拥有父资源的资源) (add)
参考文章:
http://www.lifelaf.com/blog/?p=156
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1728016