DRF(DjangoRestFramework) 설계방법

공식문서에 근거한 DRF 설계하기

SoniaComp
3 min readMar 29, 2021

참고

[ HTTP 요청 / 응답 설계 ]
Response
ErrorCode
/ SuccessCode
[ API 구조 설계 ]
Generic Views
- Mixins
- Generic API VIEW
ViewSets
[ API Custom ]
Serializers
Filtering
[ 인증 권한 설계 ]
Authentication
- SessionAuthentication
Permissions
출처; https://eunsukim.me/posts/django-rest-framework-tutorial

HTTP 요청 / 응답 설계

Response

Response(data, status=None, template_name=None, headers=None, content_type=None)

Test

  • 프론트엔드와 통신
    → Response 형식

API 구조 설계

Mixin

The mixin classes provide the actions that are used to provide the basic view behavior. Note that the mixin classes provide action methods rather than defining the handler methods, such as .get() and .post(), directly.

Generic Views

One of the key benefits of class-based views is the way they allow you to compose bits of reusable behavior. REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.

ViewSets

Github 코드 보러가기

a type of class-based Views, that does not provide any method handlers.

  • GenericViewSet
    The GenericViewSet class inherits from GenericAPIView, and provides the default set of get_object, get_queryset methods and other generic view base behavior, but does not include any actions by default. In order to use a GenericViewSet class you'll override the class and either mixin the required mixin classes, or define the action implementations explicitly.
  • ModelViewSet
    The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes. The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy().

API Custom

Serializers

  • serializer_class
    [ GenericAPIView의 Attribute 의 Basic Setting ]
    The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the get_serializer_class() method.
  • ModelSerializer
    The ModelSerializer class provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the Model fields.
    The ModelSerializer class is the same as a regular Serializer class, except that:
    1) It will automatically generate a set of fields for you, based on the model.
    2) It will automatically generate validators for the serializer, such as unique_together validators.
    3) It includes simple default implementations of .create() and .update().

Filtering

[ django-fillter ]

FilterSet creation can be customized by overriding the following methods on the backend class:

  • .get_filterset(self, request, queryset, view)
  • .get_filterset_class(self, view, queryset=None)
  • .get_filterset_kwargs(self, request, queryset, view)

인증 권한 설계

Authentication

Permissions

--

--

SoniaComp
SoniaComp

Written by SoniaComp

Data Engineer interested in Data Infrastructure Powering Fintech Innovation (https://www.linkedin.com/in/sonia-comp/)

No responses yet