Queryset
쿼리셋(쿼리 집합)은 데이터베이스의 데이터 모음집입니다.
쿼리 집합은 객체 목록으로 구성됩니다 :)
메소드 핸들러는 get_queryset 및 get_object메서드를 호출하여 요청된 모델 객체를 검색한다.
💡 get_queryset은 쿼리 집합을 가져오는 것이고 get_object는 특정 객체를 가져오는 것입니다.

Create 메서드는 get_object가 필요하지 않으므로 따로 get_object 메서드르 호출하지 않는다.
get_object는 위 이미지처럼 retrieve, udpate, destory 등 상세 경로에만 호출됩니다 :)
메소드 핸들러는 ModelSerializer 클래스를 인스턴스화 하고 요청 객체 및 모델을 전달한다.

메소드 핸들러는 Serializer에서 is_valid 메소드를 호출한다.
📝 create(), update(), partial_update() 같은 데이터가 들어오는 메서드 요청에서 일어나게 된다.

def is_valid(self, *, raise_exception=False):
assert hasattr(self, 'initial_data'), (
'Cannot call `.is_valid()` as no `data=` keyword argument was '
'passed when instantiating the serializer instance.'
)
if not hasattr(self, '_validated_data'):
try:
# _validated_data 객체가 없다면 run_validaion을 실행한다.
self._validated_data = self.run_validation(self.initial_data)
except ValidationError as exc:
# 유효성 검사가 완료된 데이터를 담아 놓는다.
self._validated_data = {}
self._errors = exc.detail
else:
self._errors = {}
if self._errors and raise_exception:
raise ValidationError(self.errors)
return not bool(self._errors)
hasattr
- hasattr는 주어진 이름의 속성이
객체에 있는지 확인하는 파이썬 내장 함수 입니다.- 해당 속성이 존재하면 True를 없다면 False를 반환합니다.
def run_validation(self, data=empty):
"""
Validate a simple representation and return the internal value.
The provided data may be `empty` if no representation was included
in the input.
May raise `SkipField` if the field should not be included in the
validated data.
"""
# 데이터가 비어있는 지 확인한다.
(is_empty_value, data) = self.validate_empty_values(data)
if is_empty_value:
return data
# 원시 데이터를 Python 데이터 유형으로 변환 <- 아래 ✅ 표시 확인
value = self.to_internal_value(data)
# Python 데이터 유형으로 변환된 값에 대해 유효성 검사 실시
self.run_validators(value)
return value
def to_internal_value(self, data):
"""
Transform the *incoming* primitive data into a native value.
✅ 수신한 원시 데이터를 Native Value로 변환합니다.
"""
raise NotImplementedError(
'{cls}.to_internal_value() must be implemented for field '
'{field_name}. If you do not need to support write operations '
'you probably want to subclass `ReadOnlyField` instead.'.format(
cls=self.__class__.__name__,
field_name=self.field_name,
)
)
메서드 핸들러는 메서드의 유형에 따라 perform_create, perform_update 과 같은 메서드를 호출합니다.
is_valid 메서드 호출 후 유효성 검사를 성공적으로 마쳤기에 -> 여기서 데이터베이스 쿼리가 수행됩니다.

마지막으로 메서드 핸들러는 DRF 응답 인스턴스를 생성합니다
destroy를 제외한 모든 핸들러는 모델 객체를 반환합니다.
그러므로 객체를 직렬화 하거나 JSON과 같은 형식으로 변환해야 합니다.
이를 위해 메소드 핸들러는 .data 프로퍼티에 액세스합니다. 이 후 모델 객체로 변환하는 to_representation() 메서드를 호출합니다.
이 후 랜더러 클래스에 전달되고 이를 필요한 형식으로 직렬화하게 됩니다
