LoginSignup
3
1

More than 5 years have passed since last update.

Django REST Framework でのCRUD実装時の基本構成 CreateAPIView

Last updated at Posted at 2018-01-19

転載元ブログ情報

この記事は自分のブログの転載です。是非遊びに来てください〜

Django REST Framework メモ :: CRUD :: CreateAPIView
http://k-mawa.hateblo.jp/entry/2018/01/19/014627

参考:公式リファレンス
[http://www.django-rest-framework.org/#api-guide:embed:cite]

Django REST Framework の基本構成メモです。
CRUDのうちのCreateの構成です

  • Django2

api/urls.py

from django.urls import path
from .views import (
    [クラス名]CreateAPIView,
    )

urlpatterns = [
   path('creating_api', [クラス名]CreateAPIView.as_view(), name='creating_api')
]

※引数とpath設定がDjango1系とは異なります

Django2以降の変更点で気づいたポイント:引数つきのpath設定 - まわ

api/serializers.py

from rest_framework.serializers import (
    ModelSerializer,
    )


from [アプリ名].models import *

class □[クラス名]CreateSerializer(ModelSerializer):
    class Meta:
        model = [クラス名]
        fields = [
            '■[フィールド名]■',
            '■[フィールド名]■',
        ]

api/views.py

from rest_framework.generics import (
    CreateAPIView
    )

from [アプリ名].models import *

from .serializers import (
    [クラス名]CreateSerializer, 
    )

class □[クラス名]CreateAPIView(CreateAPIView):
    queryset = [クラス名].objects.all()
    serializer_class = [クラス名]CreateSerializer

基本構成はこんな感じです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1