Django REST framework [제1장~제3장]을 해 보았다
Tutorial 1: Serialization
urls.py , api.py , views.py , models.py , serializer.py
Model의 속성에 포함되지 않는 요소를 Serialize 시에 포함하는 것은 가능, 예를 들어
Model이때
ModelSerializerserializers.ReadOnlyField(source='user') 에서 serializers.PrimaryKeyRelatedField()로 당깁니다. 
Tutorial 2: Requests and Responses
이 장에서는 function based view(@api_viewデコレータ) 의 구현.
return Response(data) 를 사용하면 content negotiation (determine the correct content type to return to the client) 를 쉽게 실현할 수 있다. snippets/views.py
def snippet_list(request, format=None):
snippets/urls.py
urlpatterns = format_suffix_patterns(urlpatterns)
HTTP request bodyのAccept:xxx/yyy 인가 endpointの拡張子 로 한다. # Request JSON
http http://127.0.0.1:8000/snippets/Accept:application/json
# Request HTML
http http://127.0.0.1:8000/snippets/ Accept:text/html
# JSON suffix
http http://127.0.0.1:8000/snippets.json
# Browsable API suffix
http http://127.0.0.1:8000/snippets.api
(요점 확인)
html = apiTutorial 3: Class based views
이 장에서는 Class-based Views(APIViewを継承)로 구현한다. 3단계에 걸쳐 점점 편하게 구현하는 방법을 배웠다.
APIView 상속generics.GenericAPIView 및 mixins.XxxxModelMixin를 상속.generics.ListCreateAPIView 을 계승. 결과, 여기까지 간단하게 되었다.
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import generics
class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
Reference
이 문제에 관하여(Django REST framework [제1장~제3장]을 해 보았다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kudojp/items/7ba99f2309af440d6b18텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)