<HOXY LUSH? (5)>

On a regular basis·2021년 5월 17일
1

Project_HOXYLUSH?

목록 보기
5/6
post-thumbnail

🦖 1차 프로젝트 <HOXY LUSH?> 🦖

🐳 초안과 최종본 사이에서 내가 배운 것! 🐳

-> 내가 구현한 건 ProductDetailView만!

  1. 처음에는 for 문을 돌려야하는 키값들을 3가지로 나눠서 각각 리스트에 어펜드 해주는 식으로 코드를 짰다. 그래서 맨마지막 result에 그 list값을 넣어주는 방법으로 리턴을 해줬다. 최종본에는 리스트 컴프렌션을 써서 for문을 한줄로 만들어주고 result안에 값을 다 넣어주니 코드 효율성이 올라갔고 가독성도 좋아졌다.

  2. 초안에서 filter를 주고 해당 product를 찾는 방법으로 코드를 짰었는데 최종본에서는 모두 set을 써서 _set.all()로 좀더 심플하게 코드를 정리했다.

  3. product option값들도 한개가 아니라 여러가지 값이기 때문에 for문을 돌려주어야하고, product에서 productoption의 price, quantity, weight값 한개씩만 set을 통해 가져오고 싶었는데 set을 쓰려면 값을 특정해주어야만 가능하다고 하셨다.

🐼 제품 상세페이지 view.py 초안 🐼

import json
from django.http import JsonResponse
from django.views import View

from products.models import Product, ProductImage, ProductOption, ProductDescription, Ingredient

class ProductDetailView(View):
    def get(self, request, product_id):
        product = Product.objects.get(id=product_id)

        product_images = []
        productimages = ProductImage.objects.filter(product=product)
        for productimage in productimages:
            product_images.append(productimage.image_url)

        product_descriptions = []
        productdescriptions = product.productdescription_set.all()
        for productdescription in productdescriptions:
            product_descriptions.append({
                 'description1' : productdescription.description,
                 'image_url1' : productdescription.image_url
                 })

        ingredient_detail = []
        ingredients = product.ingredient_set.all()
        for ingredient in ingredients:
            ingredient_detail.append({
                        'description2' : ingredient.description,
                        'image_url2' : ingredient.image_url,
                        'name2' : ingredient.name
                     })

        result = {
            'id'        : product.id,
            'name'      : product.name,
            'hashtag'   : product.hashtag,
            'hit'       : product.hit,
            'video_url' : product.video_url,
            'price'     : product.productoption_set.first().price,
            'weight'    : product.productoption_set.first().weight,
            'quantity'  : product.productoption_set.first().quantity,
            'product_images' : product_images,
            'product_descriptions' : product_descriptions,
            'ingredient_detail' : ingredient_detail
        }
        
        
        return JsonResponse({'result' : result}, status=200)

🐼 제품 상세페이지 view.py 최종 🐼

import json

from django.http     import JsonResponse
from django.views    import View
from django.db.models       import Q

from products.models import Category, Product, ProductImage, ProductOption, ProductDescription, Ingredient, Tag, SubCategory, ProductTag

class ProductListView(View):
    def get(self, request):
        category_id       = request.GET.get('category_id')
        sub_category_id   = request.GET.get('sub_category_id')
        keyword           = request.GET.get('keyword')
        pagination        = int(request.GET.get('pagination', 0))
        limit             = int(request.GET.get('limit', 4))
        offset            = pagination * 4
        
        if category_id or sub_category_id:
            products = Product.objects.filter(
                Q(category_id = category_id) |
                Q(sub_category_id = sub_category_id))[offset:offset+limit]

        elif keyword:
            products= Product.objects.filter(name__contains = keyword)
            
        else:
            products = Product.objects.all()[offset:offset+limit]

        product_list = [{
            'id'            : product.id,
            'name'          : product.name,
            'hashtag'       : product.hashtag,
            'option' : [{ 
                'option_id'     : option.id,
                'price'         : option.price,
                'quantity'      : option.quantity,
                'weight'        : option.weight,
                } for option in product.productoption_set.all()],
            'image_url'     : product.productimage_set.first().image_url,
            'tag'    : [{'id' : tag.id, 'tag': tag.name} for tag in product.tag_set.all()]
            } for product in products]
        
        return JsonResponse({'product_info' : product_list}, status = 200)

class ProductDetailView(View):
    def get(self, request, product_id):
        product = Product.objects.get(id=product_id)

        result = {
            'product_id'           : product.id,
            'name'                 : product.name,
            'hashtag'              : product.hashtag,
            'hit'                  : product.hit,
            'video_url'            : product.video_url,
            'product_options'      : [{ 
                'price'         : productoption.price,
                'quantity'      : productoption.quantity,
                'weight'        : productoption.weight
                } for productoption in product.productoption_set.all()],
            'product_images'       : [productimage.image_url
                  for productimage in product.productimage_set.all()],
            'product_descriptions' : [{
                    'description1' : productdescription.description,
                    'image_url1'   : productdescription.image_url
                } for productdescription in product.productdescription_set.all()],
            'ingredient_detail'    : [{
                    'description2' : ingredient.description,
                    'image_url2'   : ingredient.image_url,
                    'name2'        : ingredient.name
                } for ingredient in product.ingredient_set.all()],
            'tag'                  : [tag.name for tag in product.tag_set.all()]
                }
        return JsonResponse({'result' : result}, status=200)

🐳 오늘 새롭게 배운 것! 🐳

push한 커밋 삭제(가장 최근 commit을 삭제)

git reset HEAD^

깃 커밋메시지 수정

git commit –amend

강제 push

git push origin master(또는 브랜치이름) -f

브렌치 삭제

git branch -d (브랜치명)

  • 깃메인에서는 작업하지 않기(메인에서 작업하다가 브랜치파서 넘어가면 add, commit해줘야하니까...) => 메인에서 브랜치 만들고 거기서 작업!
  • push 받기 전에는 pull 먼저 다 받아주기!
profile
개발 기록

0개의 댓글