Django REST FRAMEWORK Tutorial の 빠른 시작 프로젝트의 API 環境をdocker-compose 그리고 立ち上げる

8657 단어 djangorestframework
前回の記事で 環境構築は完了した

8002 で動いている

startapp 그리고 project の中で 빠른 시작 앱을 만드세요



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

빠른 시작 페이지를 살펴보세요

rest4/rest4/の프로제크트の中には作れず

rest4/quickstart/그렇죠?

docker-compose run web django-admin startapp quickstart

Starting rest4_db_1 ... done                                                                                                                    
Creating rest4_web_run ... done  

django-admin startapp quickstartで quickstart 이라고 하는 이름을 앞세워 실행합니다.

======

이주하다




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.


DB の初期化完了.

=====

슈퍼 유저 생성 でスーパーユーザーを作る




dc run web \
python manage.py createsuperuser \
--email [email protected] --username admin

Creating rest4_web_run ... done
Password: 
Password (again): 
Superuser created successfully.


=====

シリアライザ、뷰、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']


ここは츄트리알통리

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

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]


츄트리알의 산풀로

from tutorial.quickstart.serializers


になっているが,

from quickstart.serializers


にする

==========

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'))
]


同様に

=====

~째





これで 작곡 で動かせる

좋은 웹페이지 즐겨찾기