adidas clone 프로젝트 과정(2)

JM·2020년 7월 19일
0

Project

목록 보기
3/11

product.models.py

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"
  • modeling한 관계도를 보며 models.py를 구현하였다.
  • 초기 관계도와는 많이 달라졌다.
  • Models.py 코드를 작성하며 많은 수정이 이루어졌다.
  • 하나의 상품의 경우 다양한 카테고리에 속해 있기 때문에 Foreignkey 설정을 하며 새롭게 이해한 부분이 많았다.
  • manytomany관계에 있는 size, color 부분이 처음엔 이해가 잘되지 않았으나 하나의 신발이 다양한 size, color를 가질 수 있고 반대로도 가능하다고 생각하니 관계가 이해되었다.
  • 처음으로 models.py를 하며 생각보다 많은 시간이 들지 않아 쉽게 생각하였으나 결과적으로 많은 수정을 통해 많은 시간을 사용하였다.

0개의 댓글