본문 바로가기

drf9

[Django] DRF - 1차 프로젝트 리팩토링 (1) Product detail & list GET Udemy로 DRF 강의를 다 듣고서, 1차 프로젝트 코드를 DRF 코드로 변경하는 작업을 진행하고 있다. 1. Product Detail GET # serializer class ProductSerializer(serializers.ModelSerializer): class Meta: model = Product fields = '__all__' # view class ProductDetailGV(generics.RetrieveAPIView): queryset = Product.objects.all() serializer_class = ProductSerializer product detail GET을 구현하기 위해서 generics.RetrieveAPIView를 사용했다. GET과 관련해서 ListA.. 2022. 6. 7.
[Django] DRF - API Testing API Testing test code를 작성할 때, app을 생성하면 자동으로 만들어지는 test.py를 사용해도 되지만, 프로젝트의 규모가 커지면 test 폴더를 따로 만들어서 관리할 수도 있다. 주의할 점은, 새로운 test 파일이나 폴더를 만들 때 이름의 시작이 test여야 한다는 점이다. ex) test_create_account Testcase를 실행하면 django는 일시적으로 새로운 db를 생성하고, 그 안에서 모든 testcase를 실행한다. test code를 실행할 때, python manage.py test라고 하게 되는데, 이건 모든 test file을 호출하게 된다. 특정 app에 있는 test code를 실험하고 싶으면 python manage.py test user_app과.. 2022. 6. 1.
[Django] DRF 정리 - instance, validated_data, @api_view, reuiqred, ModelSerializer, Nested Serializer 최근 udemy에서 DRF 강의를 듣고 있는데, 인턴을 하면서 정확하지 않은 지식으로 사용하던 것들에 대해 정리하는 시간을 가질 수 있었다. 다시 복습할 겸 정리를 해보고자 한다. 1. instance와 valideated_data serializer 내부에서 create와 update method를 생성하는 경우가 정말 많았는데, 그중에서도 update method를 만들 때 instance와 validated_data라고 하는 parameter가 꼭 들어갔다. data를 update 하기 위해 put 또는 patch 요청을 보내면 instance에는 old value가 담기게 되고, validated_date에는 new value가 담기게 된다. class MovieSerializer(serializ.. 2022. 5. 29.
[Django] DRF - Token Authentication Authentication Django Rest Framework 공식문서를 보면 authentication에 대해서 아래와 같이 말하고 있다. Note: Don't forget that authentication by itself won't allow or disallow an incoming request, it simply identifies the credentials that the request was made with. 즉 authentication은 user가 로그인했는지, 또는 valid user인지를 증명하는 역할을 한다. 어떤 level에 user가 접근할 수 있는지 없는지를 확인하는 건 permission이 한다. authentication에도 다양한 종류가 있지만 이번에는 Toke.. 2022. 5. 29.