[샤넬 프로젝트] JsonResponse

hyuckhoon.ko·2020년 6월 29일
0

What I learned in wecode

목록 보기
60/109

1. JsonResponse


JsonReponse는 HttpReposponse를 상속받는 클래스다.



2. 샤넬 프로젝트 JsonResponse


1) 샤넬 백 - (리스트) 뷰 코드 - 개선 전

 7 class BagView(View):
   8     def get(self, request):
   9
  10         total_bag_info = []
  11         bag_list = Product.objects.filter(name__contains="CHANEL 19").prefetch_related('texture', 'productimage_set')
  12
  13         for bag in bag_list:
  14             bag_code        = bag.product_code
  15             bag_img         = bag.productimage_set.all()
  16             if bag_img.exists():
  17                 bag_img         = bag_img[0].url
  18                 bag_name        = bag.name
  19                 bag_price       = bag.price
  20                 each_bag        = Product.objects.get(product_code=bag_code)
  21                 textures        = each_bag.texture.all()
  22                 texture_list    = []
  23                 bag_info = {}
  24                 for texture in textures:
  25                     texture = texture.name
  26                     texture_list.append(texture)
  27                 bag_info['bag_img'] = bag_img
  28                 bag_info['bag_name'] = bag_name
  29                 bag_info['bag_price'] = bag_price
  30                 bag_info['texture'] = texture_list
  31                 total_bag_info.append(bag_info)
  32
  33         return JsonResponse({'bag_info':total_bag_info}, status=200)


2) 샤넬 백 - (리스트) 뷰 코드 - 개선 후

  11 class BagView(View):
  12     def get(self, request):
  13         total_bag_info    = []
  14         collection_option = request.GET.getlist('collection_id__in')
  15         theme_option      = request.GET.getlist('theme__in')
  16         shape_option      = request.GET.getlist('shape__in')
  17         material_option   = request.GET.getlist('material__in')
  18
  19         filters                      = {}
  20         filters['collection_id__in'] = collection_option
  21         filters['theme__in']         = theme_option
  22         filters['shape__in']         = shape_option
  23         filters['material__in']      = material_option
  24
  25         new_filters = {}
  26         for key, value in filters.items():
  27             if value:
  28                 new_filters[key] = value
  29
  30         bag_list = Product.objects.filter(
  31             name__contains="CHANEL 19"
  32         ).prefetch_related(
  33                             'texture',
  34                             'productimage_set'
  35         ).exclude(id__lt=400).filter(**new_filters)
  36
  37         total_bag_info = [
  38             {
  39                 'bag_code'          : bag.product_code.replace(' ',''),
  40                 'bag_img'           : bag.productimage_set.all()[0].url,
  41                 'bag_name'          : bag.name,
  42                 'bag_price'         : bag.price,
  43                 'texture'           : [texture.name for texture in bag.texture.all()]
  44             } for bag in bag_list
  45         ]
  46         return JsonResponse({'bag_info':total_bag_info}, status=200)


3) Postman 결과

0개의 댓글