샵과 샵이 아닌거 비교하면서 내 예전 코드 발전되 코드 점점 변동기를 보여주자!
import json
from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
from django.db.models import Q
from products.models import Product, Category, SubCategory, ProductImage, Tag, ProductTag
# 프론트가 원하는 데이터
# 메인페이지: 상품ID, 상품명, 상품 설명(해쉬태그형식),가격, 상품이미지
# 제품리스트: 이미지, 태그, 제품명, 해시태그, 중량, 가격, 재고,
# <프론트랑 연동 시험할 것>
class ProductListView(View):
def get(self, request):
products = Product.objects.all()
pagination = int(request.GET.get('pagination', 0))
limit = int(request.GET.get('limit', 0))
offset = int(request.GET.get('offset', 0))
product_list = [
{
'product_id' : product.id,
'name' : product.name,
'hashtag' : product.hashtag,
'option' : [{
'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' : [{
'tag' : tag.name,
'id' : tag.id,
} for tag in product.tag_set.all()]
} for product in products]
#q 객체 써볼 것!
# When(Q(name__startswith="John") | Q(name__startswith="Paul"), then='name')
# product_all = User.objects.filter(
# Q(first_name__startswith='R') | Q(last_name__startswith='D')
# )
# When(Q(pagination=0) |
# Q(pagination=1) |
# Q(pagination=2) |
# Q(pagination=3) |
# Q(pagination=4) |
# Q(pagination=5), then(product_all=product_list[offset:offset+limit]))
if pagination == 0:
sorted_products = product_list[offset:offset+limit]
elif pagination == 1:
sorted_products = product_list[offset:offset+limit]
elif pagination == 2:
sorted_products = product_list[offset:offset+limit]
elif pagination == 3:
sorted_products = product_list[offset:offset+limit]
elif pagination == 4:
sorted_products = product_list[offset:offset+limit]
elif pagination == 5:
sorted_products = product_list[offset:offset+limit]
return JsonResponse({'product_info' : sorted_products}, status = 200)
class ProductFilterView(View):
def get(self, request):
category_id = request.GET.get('category_id')
sub_category_id = request.GET.get('sub_category_id')
pagination = int(request.GET.get('pagination', 0))
limit = int(request.GET.get('limit', 0))
offset = int(request.GET.get('offset', 0))
if category_id or sub_category_id:
products = Product.objects.filter(
Q(category_id = category_id) |
Q(sub_category_id = sub_category_id))
product_list = [
{
'product_id' : product.id,
'name' : product.name,
'hashtag' : product.hashtag,
'option' : [{
'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' : [tag.name for tag in product.tag_set.all()],
} for product in products]
if pagination == 0:
sorted_products = product_list[offset:offset+limit]
elif pagination == 1:
sorted_products = product_list[offset:offset+limit]
elif pagination == 2:
sorted_products = product_list[offset:offset+limit]
elif pagination == 3:
sorted_products = product_list[offset:offset+limit]
return JsonResponse({'product_info' : sorted_products}, status = 200)
products = Product.objects.all()
product_list = [
{
'product_id' : product.id,
'name' : product.name,
'hashtag' : product.hashtag,
'option' : [{
'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' : [tag.name for tag in product.tag_set.all()],
} for product in products]
if pagination == 0:
sorted_products = product_list[offset:offset+limit]
elif pagination == 1:
sorted_products = product_list[offset:offset+limit]
elif pagination == 2:
sorted_products = product_list[offset:offset+limit]
elif pagination == 3:
sorted_products = product_list[offset:offset+limit]
return JsonResponse({'product_info' : sorted_products}, status = 200)
class CategoryView(View):
def get(self, request):
categories = Category.objects.all()
category_list = [
{
'category' : category.id,
'title' : category.title,
}
for category in categories]
return JsonResponse({'category_list' : category_list}, status = 200)
class SubCategoryView(View):
def get(self, request):
sub_categories = SubCategory.objects.all()
sub_category_list = [
{
'sub_category' : sub_category.id,
'sub_title' : sub_category.sub_title,
}
for sub_category in sub_categories]
return JsonResponse({'sub_category_list' : sub_category_list}, status = 200)
# q객체 서치 시도해본 것.
# product_condition = Q()
# if category:
# product_filter.add(Q(category__sub_category=category), Q.AND)
# if sub_category:
# product_filter.add(Q(sub_category=sub_category), Q.AND)
# if items_count:
# product_filter.add(Q(__category=category),category.category_set.count()
# filtered_products = Product.objects.filter(condition).distinct()
# # #카운트 함수써서 총 개수 (n) 데이터값 보내주기
# return JsonResponse({'product_info' : filtered_products}, status = 200)
#검색창 기능
# filter(name__contains)
import json
from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
from django.db.models import Q
from products.models import Category, Product, ProductImage, SubCategory, ProductTag, Tag
class ProductListView(View):
def get(self, request):
category_id = request.GET.get('category_id')
sub_category_id = request.GET.get('sub_category_id')
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]
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)