利用Django實現RESTful API
RESTful API現在很流行,這裏是它的介紹 理解RESTful架構和 RESTful API設計指南.按照Django的常規方法當然也可以實現REST,但有一種更快捷、強大的方法,那就是 Django REST framework.它是python的一個模塊,通過在Django裏面配置就可以把app的models中的各個表實現RESTful API。下面是實現方法:
一、安裝配置
pip install djangorestframework pip install markdown # Markdown support for the browsable API.pip install django-filter # Filtering support
再到Django的 settings.py 中的INSTALLED_APPS
添加 rest_framework,如下:
1234 | INSTALLED_APPS = ( ... 'rest_framework' , ) |
在根目錄的 url.py 文件中為rest_framework
框架的 login 和 logout 視圖添加url:
1234 | urlpatterns = [ ... url(r '^api-auth/' , include( 'rest_framework.urls' , namespace = 'rest_framework' )) ] |
二、創建model和Serializer
創建app,名為 snippets
.。在視圖 models.py 中添加一張表如下:
from django.db import modelsfrom pygments.lexers import get_all_lexers # 一個實現代碼高亮的模塊 from pygments.styles import get_all_styles LEXERS = [item for item in get_all_lexers() if item[1]] LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS]) # 得到所有編程語言的選項 STYLE_CHOICES = sorted((item, item) for item in get_all_styles()) # 列出所有配色風格class Snippet(models.Model): created = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=100, blank=True, default='') code = models.TextField() linenos = models.BooleanField(default=False) language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100) style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100) class Meta: ordering = ('created',)
然後開始同步到數據庫中:
./manage.py makemigrations snippets ./manage.py migrate
接下來需要做的就是創建 Serializer 類,類似於 Form。它的作用就是從你傳入的參數中提取出你需要的數據,並把它轉化為 json 格式(註意,已經是字節碼了),同時支持反序列化到model對象。在 snippets 文件夾中添加
serializers.py
並在其添加如下:
from rest_framework import serializersfrom snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES class SnippetSerializer(serializers.Serializer): # 它序列化的方式很類似於Django的forms id = serializers.IntegerField(read_only=True) title = serializers.CharField(required=False, allow_blank=True, max_length=100) code = serializers.CharField(style={'base_template': 'textarea.html'}) # style的設置等同於Django的 widget=widgets.Textarea linenos = serializers.BooleanField(required=False) # 用於對瀏覽器的上的顯示 language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python') style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly') def create(self, validated_data): """ Create and return a new `Snippet` instance, given the validated data. """ return Snippet.objects.create(**validated_data) def update(self, instance, validated_data): """ Update and return an existing `Snippet` instance, given the validated data. """ instance.title = validated_data.get('title', instance.title) instance.code = validated_data.get('code', instance.code) instance.linenos = validated_data.get('linenos', instance.linenos) instance.language = validated_data.get('language', instance.language) instance.style = validated_data.get('style', instance.style) instance.save() return instance
三、使用Serializer
先使用 ./manage.py shell 進入Django的shell中。操作如下:
可以看到 Serializer 的使用如同 Django 的 forms.它的反序列化如下:
from django.utils.six import BytesIO stream = BytesIO(content) data = JSONParser().parse(stream)
這是再把得到的數據轉化為實例:
serializer = SnippetSerializer(data=data) serializer.is_valid() # 開始驗證 # Trueserializer.validated_data # OrderedDict([('title', ''), ('code', 'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])serializer.save() # <Snippet: Snippet object>
同時,我們還可以對 querysets 進行序列化,只需簡單地在設置參數 many=True,如下:
serializer = SnippetSerializer(Snippet.objects.all(), many=True) serializer.data # [OrderedDict([('id', 1), ('title', u''), ('code', u'foo = "bar"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 2), ('title', u''), ('code', u'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 3), ('title', u''), ('code', u'print "hello, world"'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
四、使用 ModelSerializer
ModelSerializer
類似於Django的 modelform, 可以直接關聯到models中的表。如下:
class SnippetSerializer(serializers.ModelSerializer): class Meta: model = Snippet fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
五、在Django的視圖中使用Serializer
首先,可以像常規Django視圖的寫法一樣寫,返回序列化的輸出數據。
from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from snippets.models import Snippet from snippets.serializers import SnippetSerializer @csrf_exemptdef snippet_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Snippet.objects.all() serializer = SnippetSerializer(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
也可以寫一個視圖對應其models中的表,實現對它的刪、改、查。
@csrf_exemptdef snippet_detail(request, pk): """ Retrieve, update or delete a code snippet. """ try: snippet = Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = SnippetSerializer(snippet) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = SnippetSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': snippet.delete() return HttpResponse(status=204)
添加對應的url, snippets/urls.py 中設置如下:
from django.conf.urls import urlfrom snippets import views urlpatterns = [ url(r'^snippets/$', views.snippet_list), url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail), ]
最後還要在根目錄的 url.py 中添加對應的映射。
urlpatterns = [ ... url(r'^', include('snippets.urls')), ]
這時,所有的配置已經完成了。接下來就是測試我們的API
六、測試API
為了方便我們可以使用 httpie 模塊來測試,啟動Django,再在客戶端輸入
http://127.0.0.1:8000/snippets/,操作如下:
還可以進行 put 操作,修改對應的內容
利用Django實現RESTful API