본문 바로가기

분류 전체보기23

[Django] DRF - 1차 프로젝트 리팩토링 (4) API Testing API 부분의 리팩토링이 어느 정도 끝난 후 test code를 작성했다. 1. Category & Product 먼저 category와 product의 detail과 list를 get에 관련한 테스트 코드를 작성했다. # category class CategoryTestCase(APITestCase): def setUp(self): self.category = Category.objects.create( category_name = 'test category', main_description = 'test main description', sub_description = 'test sub description' ) def test_category_list_get(self): response = self.. 2022. 6. 14.
[Django] DRF - 1차 프로젝트 리팩토링 (3) Cart 리팩토링을 진행하면서 cart 부분이 가장 시간이 오래 걸렸다. 1차 프로젝트 당시에 cart 부분을 담당하지 않아서 이해하는데 시간이 걸린 것도 있지만, view 또는 serializer 둘 중 어디서 create 또는 post를 적용시켜야 할지 오랜 고민을 하기도 했다. 몇 번의 시도 끝에 3일 만에 기능을 구현할 수 있었다. 1. CreateAPIView 활용 처음에는 generic views를 사용해서 구현했다. # serializer class CartSerializer(serializers.ModelSerializer): user = serializers.StringRelatedField(read_only=True) product = serializers.StringRelatedField(re.. 2022. 6. 9.
[Django] DRF - 1차 프로젝트 리팩토링 (2) Category detail & list GET 1. Category List GET product GET과 유사하지만, 기존 코드에 limit, offset이 걸려 있어서 이번엔 pagination.py 파일을 따로 만들어 offset limit을 설정했다. # pagination from rest_framework.pagination import LimitOffsetPagination class CategoryLimitOffsetPagination(LimitOffsetPagination): default_limit = 5 # serializer class CategorySerializer(serializers.ModelSerializer): class Meta: model = Category fields = '__all__' # view class.. 2022. 6. 7.
[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.