github.com/encode/django-rest-framework/blob/master/rest_framework/mixins.py
를 기준으로 알아보자
1. RetrieveModelMixin
class RetrieveModelMixin:
"""
Retrieve a model instance.
"""
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
에서 첫번째 줄
instance = self.get_object()가 실행되면
TblistRecommend object (38) 가 리턴된다
마찬가지로 get_queryset()도 (docs.djangoproject.com/en/3.1/ref/class-based-views/mixins-single-object/)
- get_object는 (velog.io/@jcinsh/RetrieveUpdateDestroyView-%EC%9D%B4%ED%95%B4)
- queryset과 pk값을 인자로 받아서,
- queryset.filter(pk=pk)로 queryset을 뽑고,
- obj = queryset.get()으로 객체만 뽑아서 리턴해 주는 메소드임
=> 결국, 위 코드는 Customer.objects.get(pk=pk) 리턴함
- get_serializer는
- instance을 인자로 받아서, 해당 객체을 serialize해 주는 메소드임
get_object(queryset=None)¶
Returns the single object that this view will display. If queryset is provided, that queryset will be used as the source of objects; otherwise, get_queryset() will be used. get_object() looks for a pk_url_kwarg argument in the arguments to the view; if this argument is found, this method performs a primary-key based lookup using that value. If this argument is not found, it looks for a slug_url_kwarg argument, and performs a slug lookup using the slug_field.
When query_pk_and_slug is True, get_object() will perform its lookup using both the primary key and the slug.
get_queryset()¶
Returns the queryset that will be used to retrieve the object that this view will display. By default, get_queryset() returns the value of the queryset attribute if it is set, otherwise it constructs a QuerySet by calling the all() method on the model attribute’s default manager.
def get_object(self, queryset=None):
"""
Returns the object the view is displaying.
By default this requires `self.queryset` and a `pk` or `slug` argument
in the URLconf, but subclasses can override this to return any object.
"""
# Use a custom queryset if provided; this is required for subclasses
# like DateDetailView
if queryset is None:
queryset = self.get_queryset()
# Next, try looking up by primary key.
pk = self.kwargs.get(self.pk_url_kwarg, None)
slug = self.kwargs.get(self.slug_url_kwarg, None)
if pk is not None:
queryset = queryset.filter(pk=pk)
# Next, try looking up by slug.
if slug is not None and (pk is None or self.query_pk_and_slug):
slug_field = self.get_slug_field()
queryset = queryset.filter(**{slug_field: slug})
# If none of those are defined, it's an error.
if pk is None and slug is None:
raise AttributeError("Generic detail view %s must be called with "
"either an object pk or a slug."
% self.__class__.__name__)
try:
# Get the single item from the filtered queryset
obj = queryset.get()
except queryset.model.DoesNotExist:
raise Http404(_("No %(verbose_name)s found matching the query") %
{'verbose_name': queryset.model._meta.verbose_name})
return obj # Customer.object.get(pk=pk) 의미
# get_serializer 소스 코드
def get_serializer(self, instance=None, data=None,
files=None, partial=False):
"""
Return the serializer instance that should be used for validating and
deserializing input, and for serializing output.
"""
serializer_class = self.get_serializer_class()
context = self.get_serializer_context()
return serializer_class(instance, data=data, files=files,
partial=partial, context=context)
www.django-rest-framework.org/api-guide/serializers/#accessing-the-initial-data-and-instance
velog.io/@jcinsh/DRF-14-DRF-%EA%B3%B5%EC%8B%9D%EB%AC%B8%EC%84%9C-%EC%9A%94%EC%95%BD
'코딩 > 장고' 카테고리의 다른 글
장고 api 모니터링 (0) | 2021.02.22 |
---|---|
django nested prefetch_related (0) | 2021.02.22 |
django model reg_user, update_user (0) | 2021.02.04 |
django restframework + vuejs + kakao login (0) | 2021.01.28 |
elasticsearch-django 연동 ted sample (0) | 2021.01.21 |