728x90
반응형
set up environment
mkdir tutorial
cd tutorial
python3 -m venv venv
source venv/bin/activate
install frameworks
pip install django
pip install djangorestframework
create project and app
django-admin startproejct tutorial
cd tutorial
django-admin startapp quickstart
# also, the below can be used to start an app
python3 manage.py startapp quickstart
migrate
python3 manage.py migrate
create super user
python3 manage.py createsuperuser --email admin@apple.com --username admin
serializers
define serializers
# app - 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']
Grouping views together all the common behavior into classes called ViewSets
# app - views.py
from django.shortcuts import render
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from quickstart.serializers import UserSerializer, GroupSerializer
# Create your views here.
class UserViewSetv(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited
"""
queryset = User.objects.all().order_by('date_joined')
serializer_class = UserSerializer
permission_classses = [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]
Generating URL conf for APIs by registering the viewsets with a router class
# project - 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)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Control how many objects per page are returned
adding rest_framework into third party apps section
# project - settings.py
# Quick start for DRF
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS" : "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE" : 10,
}
...
## Third party Apps
INSTALLED_APPS += [
'rest_framework',
]
Run server in order to test it
python3 manage.py runserver
bash: curl -H 'Accept: application/json; indent=4' -u admin:password123 <http://127.0.0.1:8000/users/>
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"email": "admin@example.com",
"groups": [],
"url": "<http://127.0.0.1:8000/users/1/>",
"username": "admin"
},
]
}
bash: http -a admin:password123 <http://127.0.0.1:8000/users/>
HTTP/1.1 200 OK
...
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"email": "admin@example.com",
"groups": [],
"url": "<http://localhost:8000/users/1/>",
"username": "paul"
},
]
}
728x90
반응형
'Dev. > Django & DRF' 카테고리의 다른 글
Django : DRF 적용하기2 (0) | 2023.08.16 |
---|---|
Django : DRF 적용하기 (0) | 2023.08.15 |
Django : 시작하기 (0) | 2023.08.09 |
Django : 프로그램 구조 이해하기 (0) | 2023.08.02 |
Django : 장고에 대해서 (0) | 2023.07.22 |
댓글