DEV Community

kaede
kaede

Posted on • Updated on

Django REST FRAMEWORK Tutorial 1 -- quickstart アプリの API 環境をdocker-compose で 立ち上げる

前回の記事で 環境構築は完了した

8002 で動いている

startapp で project の中で quickstart アプリを作る

https://www.django-rest-framework.org/tutorial/quickstart/#project-setup

quickstart のページを見る

rest4/rest4/ のプロジェクトの中には作れず

rest4/quickstart/ という構成になる。しかし問題はない。

docker-compose run web django-admin startapp quickstart

Starting rest4_db_1 ... done                                                                                                                    
Creating rest4_web_run ... done  
Enter fullscreen mode Exit fullscreen mode

django-admin startapp quickstart で quickstart という名前のアプリができる。

======

migrate する

docker-compose run web python manage.py migrate

Creating rest4_web_run ... done
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
Enter fullscreen mode Exit fullscreen mode

DB の初期化完了。

=====

create super user でスーパーユーザーを作る

dc run web \
python manage.py createsuperuser \
--email admin@example.com --username admin

Creating rest4_web_run ... done
Password: 
Password (again): 
Superuser created successfully.
Enter fullscreen mode Exit fullscreen mode

dc は docker-compose のエイリアス。

=====

シリアライザ、ビュー、URL を作る

serializers.py

from django.contrib.auth.models import User, Group
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name']
Enter fullscreen mode Exit fullscreen mode

ここはチュートリアル通り

============

views.py

from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from quickstart.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]
Enter fullscreen mode Exit fullscreen mode

チュートリアルのサンプルでは

from tutorial.quickstart.serializers
Enter fullscreen mode Exit fullscreen mode

になっているが、これだと lint に間違っていると指摘されたので

from quickstart.serializers
Enter fullscreen mode Exit fullscreen mode

にする 

==========

urls.py

from django.urls import include, path
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Enter fullscreen mode Exit fullscreen mode

同様に

=====

まとめ

Image description

これで compose で動かせる

Top comments (0)