DRF(DjangoRestFramework) 설계방법
참고
[ HTTP 요청 / 응답 설계 ]
Response
ErrorCode / SuccessCode[ API 구조 설계 ]
Generic Views
- Mixins
- Generic API VIEW
ViewSets[ API Custom ]
Serializers
Filtering[ 인증 권한 설계 ]
Authentication
- SessionAuthentication
Permissions
HTTP 요청 / 응답 설계
Response
Response(data, status=None, template_name=None, headers=None, content_type=None)
- data: serialized 된 data
- stauts: django status code 사용
- send_response, ok_response, error_response
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
a type of class-based Views, that does not provide any method handlers.
- GenericViewSet
TheGenericViewSet
class inherits fromGenericAPIView
, and provides the default set ofget_object
,get_queryset
methods and other generic view base behavior, but does not include any actions by default. In order to use aGenericViewSet
class you'll override the class and either mixin the required mixin classes, or define the action implementations explicitly. - ModelViewSet
TheModelViewSet
class inherits fromGenericAPIView
and includes implementations for various actions, by mixing in the behavior of the various mixin classes. The actions provided by theModelViewSet
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 theget_serializer_class()
method.- ModelSerializer
TheModelSerializer
class provides a shortcut that lets you automatically create aSerializer
class with fields that correspond to the Model fields.
TheModelSerializer
class is the same as a regularSerializer
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)