200622 ~ 200703 (2주)
class TotalView(View):
def get(self, request):
target_id = request.GET.get("id", None)
all_sports = Product.objects.prefetch_related('gender','sport','tag','image_set','detail','size','color').all()[:40]
product_list = [
{
'id' : shoe.id,
'name' : shoe.name,
'sport' : shoe.sport.name,
'tag' : shoe.tag.name,
'gender' : shoe.gender.name,
'use' : shoe.detail.purpose,
'original_price' : round(shoe.price),
'sale_price' : round(shoe.discount_price),
'sizes' : [size_info.name for size_info in shoe.size.all()],
'color' : [color.name for color in shoe.color.all()][0],
'main_images' : [img.img_url for img in shoe.image_set.filter(product_id=shoe.id)[:2]],
'sub_images' : [sub_img.img_url for sub_img in shoe.image_set.filter(product_id = shoe.id,is_sub_img=True)],
'color_number' : len(shoe.image_set.filter(product_id = shoe.id,is_sub_img=True))
} for shoe in all_sports]
return JsonResponse({"total_product":product_list}, status=200)
from django.db import models
from account.models import Account
from order.models import Order
class Category(models.Model):
name = models.CharField(max_length = 50)
class Meta:
db_table = "categories"
class Gender(models.Model):
name = models.CharField(max_length = 20)
class Meta:
db_table = "gender"
class Sport(models.Model):
name = models.CharField(max_length = 50)
class Meta:
db_table = "sports"
class Tag(models.Model):
name = models.CharField(max_length = 20)
class Meta:
db_table = "tags"
class Review(models.Model):
account = models.ForeignKey("account.Account", on_delete=models.SET_NULL,null =True)
product = models.ForeignKey("Product", on_delete=models.SET_NULL,null =True)
date = models.DateTimeField(auto_now_add = True)
comment = models.CharField(max_length = 200)
point = models.IntegerField(default=1)
class Meta:
db_table = "reviews"
class Product (models.Model):
category = models.ForeignKey("Category", on_delete=models.SET_NULL, null= True, related_name='category_product')
gender = models.ForeignKey("Gender", on_delete=models.SET_NULL,null =True, related_name='gender_product')
sport = models.ForeignKey("Sport", on_delete=models.SET_NULL, null =True, related_name='sport_product')
name = models.CharField(max_length=50)
tag = models.ForeignKey("Tag", on_delete=models.SET_NULL,null =True, related_name='tag_product')
price = models.DecimalField(max_digits=16, decimal_places=2)
discount_price = models.DecimalField(max_digits=16, decimal_places=2,default =0)
description = models.CharField(max_length=1500)
detail = models.OneToOneField("Detail", on_delete=models.SET_NULL , null =True, related_name='detail_product')
size = models.ManyToManyField("Size", through='ProductSize', related_name='size_product')
code = models.CharField(max_length = 50, default="")
color = models.ManyToManyField("Color", through='MiddleColorImage', related_name='colors_product')
review_account = models.ManyToManyField("account.Account", through='Review', related_name='reviews_product')
class Meta:
db_table = "products"
class Color(models.Model):
name = models.CharField(max_length=50)
class Meta:
db_table = "colors"
class MiddleColorImage(models.Model):
product = models.ForeignKey("Product", on_delete=models.SET_NULL, null =True)
color = models.ForeignKey("Color", on_delete=models.SET_NULL, null =True)
class Meta:
db_table = "middle_colorimages"
class Image(models.Model):
img_url = models.CharField(max_length=300)
product = models.ForeignKey("Product", on_delete=models.SET_NULL, null =True)
is_sub_img = models.BooleanField(default=False)
class Meta:
db_table = "images"
class Size (models.Model):
name = models.CharField(max_length=10)
class Meta:
db_table = "sizes"
class ProductSize (models.Model):
product = models.ForeignKey("Product", on_delete=models.SET_NULL,null =True)
size = models.ForeignKey("Size", on_delete=models.SET_NULL,null=True)
class Meta:
db_table = "product_sizes"
class Detail (models.Model):
code = models.CharField(max_length = 50)
purpose = models.CharField(max_length = 50)
material = models.CharField(max_length = 200)
origin = models.CharField(max_length = 50)
manufacturing_date = models.CharField(max_length = 50)
class Meta:
db_table = "detail"