👉 ERD 최종 완성본
(1) core
from django.db import models
class TimeStamp(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
abstract = True
속성을 통해 해당 모델을 추상화 하여 DB에 테이블이 생성되지 않는다. (2) users
from django.db import models
from core.models import TimeStamp
class User(TimeStamp):
email = models.CharField(max_length=100, unique=True)
name = models.CharField(max_length=100)
password = models.CharField(max_length=200)
phone_number = models.CharField(max_length=50)
nickname = models.CharField(max_length=100)
class Meta:
db_table = 'users'
class Address(TimeStamp):
user = models.ForeignKey("User", on_delete=models.CASCADE)
address = models.CharField(max_length=100)
class Meta:
db_table = 'addresses'
(3) products
from django.db import models
from core.models import TimeStamp
from users.models import User
class Menu(TimeStamp):
name = models.CharField(max_length=100)
image_url = models.CharField(max_length=100)
class Meta:
db_table = 'menus'
class Category(TimeStamp):
name = models.CharField(max_length=100)
menu = models.ForeignKey('Menu', on_delete=models.CASCADE)
class Meta:
db_table = 'categories'
class SubCategory(TimeStamp):
name = models.CharField(max_length=100)
category = models.ForeignKey('Category', on_delete=models.CASCADE)
class Meta:
db_table = 'sub_categories'
class ProductGroup(TimeStamp):
name = models.CharField(max_length=50)
company = models.CharField(max_length=50)
displayed_price = models.DecimalField(max_digits=10, decimal_places=3)
discount_rate = models.DecimalField(max_digits=5, decimal_places=3)
sub_category = models.ForeignKey('SubCategory', on_delete=models.CASCADE)
delivery = models.ForeignKey("Delivery", on_delete=models.CASCADE)
class Meta:
db_table = 'product_groups'
class Product(TimeStamp):
name = models.CharField(max_length=50)
price = models.DecimalField(max_digits=10, decimal_places=3)
product_group = models.ForeignKey('ProductGroup', on_delete=models.CASCADE)
colors = models.ManyToManyField("Color", related_name='products', through="ProductColor")
class Meta:
db_table = 'products'
class ProductImage(TimeStamp):
image_url = models.CharField(max_length=200)
product_group = models.ForeignKey('ProductGroup', on_delete=models.CASCADE)
class Meta:
db_table = 'product_images'
class ProductColor(TimeStamp):
product = models.ForeignKey('Product', on_delete=models.CASCADE)
color = models.ForeignKey('Color', on_delete=models.CASCADE)
class Meta:
db_table = 'products_colors'
class Color(TimeStamp):
name = models.CharField(max_length=50, null=True)
class Meta:
db_table = 'colors'
class Review(TimeStamp):
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.CharField(max_length=500)
star_rate = models.DecimalField(max_digits=3, decimal_places=2)
product_group = models.ForeignKey('ProductGroup', on_delete=models.CASCADE)
class Meta:
db_table = 'reviews'
class ReviewImage(TimeStamp):
image_url = models.CharField(max_length=200)
review = models.ForeignKey("Review", on_delete=models.CASCADE)
class Meta:
db_table = 'review_images'
class Delivery(TimeStamp):
delivery_type = models.CharField(max_length=50)
payment_type = models.CharField(max_length=50)
delivery_fee = models.DecimalField(max_digits=10, decimal_places=3)
class Meta:
db_table = 'deliveries'
(4) carts
from django.db import models
from django.db.models.fields import IntegerField
from core.models import TimeStamp
from users.models import User
from products.models import Product, Color
class Cart(TimeStamp):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
color = models.ForeignKey(Color, on_delete=models.CASCADE)
class Meta:
db_table = 'carts'
(5) orders
from django.db import models
from django.db.models.fields import IntegerField
from core.models import TimeStamp
from users.models import User, Address
from products.models import Product, Color
class Order(TimeStamp):
user = models.ForeignKey(User, on_delete=models.CASCADE)
address = models.ForeignKey(Address, on_delete=models.CASCADE)
status = models.ForeignKey("OrderStatus", on_delete=models.CASCADE)
class Meta:
db_table = 'orders'
class OrderItem(TimeStamp):
order = models.ForeignKey("Order", on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(Color, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
status = models.CharField(max_length=30)
class Meta:
db_table = 'order_items'
class OrderStatus(TimeStamp):
class Status(models.IntegerChoices):
BEFORE_DEPOSIT = 1
STAND_BY = 2
DEPOSIT_COMPLTED = 3
CANCEL = 4
description = models.CharField(max_length=50)
class Meta:
db_table = 'order_statuses'
class Shipment(TimeStamp):
order_item = models.ForeignKey("OrderItem", on_delete=models.CASCADE)
tracking_number = models.IntegerField(default=0)
date = models.DateField(auto_now=True)
class Meta:
db_table = 'shipments'
👉 참고 사이트 http://www.databaseanswers.org/data_models/e_commerce/index.htm